Node.js installation and configuration environment variables only read this article

1. node.js installation

After node.js is installed, it will bring the corresponding npm package management tool.

  1. Node js official website download Select the appropriate version to download.
    insert image description here
    Select the stable version here. Perform the installation step by step, during which the installation disk defaults to the C drive, it is recommended to change to the drive letter.
    I installed it on the E drive
    insert image description here

  2. Use the window + R shortcut key to start the cmd command line to verify whether node.js is installed successfully
    insert image description here

2. Node.js environment variable configuration

  1. Change the global installation path:

If you do not change the default path of the global installation, it will be installed to the path of the C drive (C:\Users\hua\AppData\Roaming\npm) by default. It is recommended to change the
node installation drive letter. In the node.js installation directory, create two Two folders node_global and node_cache are used to store installed global modules and global cache information respectively

  1. Set the global module installation path, set the global cache storage path
    After creating the two folders, enter the following command in the cmd window (the two paths are the paths of the two folders):
    insert image description here
  # 设置全局模块安装路径
  npm config set prefix "E:\Program Files\nodejs\node_global"
  # 设置全局缓存存放路径
  npm config set cache "E:\Program Files\nodejs\node_cache"

  1. Set the computer environment variables, the opening order of the environment variable interface: right click "My Computer" = "Properties = "Advanced System Settings = "Environment Variables:

before fixing:
insert image description here

After modification: After
deletion, C:\Users\Lenovo\AppData\Roaming\npmafter addition:E:\Program Files\npm_global_modules
insert image description here

Create a new system variable: NODE_PATH:E:\Program Files\nodejs\node_global
insert image description here

  1. Whether the test is successful:
    To test whether the configuration is successful, enter the following command in the cmd window Enter the following command in the cmd window to install the Vue module globally
  npm install -g vue # -g 表示全局安装

insert image description here

3. Domestic mirror website configuration

Configure domestic mirroring to solve the problem of slow or failed module installation. General configuration Taobao npm mirror

  1. In the cmd command line, configure the Taobao mirror through the command
  npm install -g cnpm --registry=https://registry.npm.taobao.org

Use the Taobao mirror to download the module, that is, replace npm with cnpm

  cnpm install # module_name
  1. Switch tool nrm installation
    Use npm to install nrm globally
  npm install nrm -g

Execution nrm ls
If
insert image description here
an error is reported during the installation process: Error [ERR_REQUIRE_ESM]: require() of ES Module D:\npm\node_modules\nrm\node_modules\open\index.js from D:\npm\node_modules\nrm\cli.js not supported.
Instead change the require of index.js in D:\npm\node_modules\nrm\cli.js to a dynamic import() which is available in all CommonJS modules. at
Object. (D:\npm\node_modules\nrm\cli.js :9:14) { code: 'ERR_REQUIRE_ESM' } Reason: The open CommonJs specification package should be used, and now open v9.0.0 is the ES Module version package



insert image description here

Solution:npm install -g nrm [email protected] --save

  1. Use the nrm ls command to view the npm warehouse list. The one with * is the currently selected mirror warehouse:
    insert image description here
    enter nrm ls in cmd, the display is as follows, and it is found that * cannot be found
    to solve the problem. Find cli.js in the installation nrm directory and open it Modify the code
    Modify the code as follows, change && to ||
    Before modification :
 if (hasOwnProperty(customRegistries, name) && (name in registries || customRegistries[name].registry === registry.registry)) {
    
    
                    registry[FIELD_IS_CURRENT] = true;
                    customRegistries[name] = registry;
                }
                setCustomRegistry(customRegistries);
                printMsg(['', '   Registry has been set to: ' + newR, '']);
            }).catch(err => {
    
    
                exit(err);
            });
        });

After modification:

 if (hasOwnProperty(customRegistries, name) || (name in registries || customRegistries[name].registry === registry.registry)) {
    
    
                    registry[FIELD_IS_CURRENT] = true;
                    customRegistries[name] = registry;
                }//修改了&&为||
                setCustomRegistry(customRegistries);
                printMsg(['', '   Registry has been set to: ' + newR, '']);
            }).catch(err => {
    
    
                exit(err);
            });
        });

Execute here:
nrm use taobao
nrm ls
insert image description here

  1. Use nrm use xxxto specify the mirror source to use:
  nrm use taobao

insert image description here

  1. Finally, test the speed nrm test npmwith

insert image description here

4. Common commands of npm, yarn, pnpm, nrm

4.1 nrm common commands:

  1. Install nrm:npm install -g nrm
  2. View nrm version number:nrm -V
  3. View current source:nrm current
  4. View source list:nrm ls
  5. Switch source:nrm use <registry> registry为源名
  6. Delete source:nrm del <registry>
  7. Test source speed:nrm test <registry>

4.2 npm common commands:

  1. Check the version number:npm -v
  2. View the global installation first-level directory:npm list -g --depth 0
  3. View the global installation path of nodejs: npm config ls
  4. Switch source: npm config set registry <url>url is the source address
例如:npm config set registry https://registry.npmjs.org/

4.3 Yarn common commands:

  1. Install command:npm install -g yarn
  2. Check the yarn version:yarn -v
  3. Uninstall yarn command:npm uninstall -g yarn

5. Regular method of uploading to npm public registry (npm publish / yarn publish)

5.1 Release npm steps:

  1. Switch the registry to the npm official registry:
npm config set registry https://registry.npmjs.org/ 
 或
nrm use npm
  1. npm registered users (if no npm account)
    go to npm official website to register
    or
npm adduser 
  1. npm login (if you already have an npm account)
npm login

Remarks: Please fill in the npm username and password for username and password, and the one-time password needs to be checked from the mailbox

  1. Query the current login account:npm whoami
  2. npm release package:npm publish

5.2 Use the yarn image source and yarn command to upload (if the npm image often fails to connect to the network, it is recommended to try yarn)

  1. Switch to the yarn image source:nrm use yarn
  2. To log in to the npm account, you also need to enter:yarn login
  3. Publish: yarn publish

Guess you like

Origin blog.csdn.net/u014212540/article/details/130260679