npm (yarn) installation and problem notes


foreword

Install node before installing npm and yarn: node —> npm —> yarn


1. Install node

1. Download node.js

1) Download and execute the installation: https://nodejs.org/en/
2) Remember the installation directory of node, it is best not to put the C drive - -. Useful later. For example, my nodejs installation directory is: E:\nodejs
3) Check whether the installation is successful:

node -v

Success will display the version number:
Command Line

Two, configure npm

1. Change the default storage location of npm packages

可以不更改,默认全局仓库就是存在C盘:C:\Users\Administrator\AppData\Roaming\npm 但是,随着npm下载模块的增多,挤占C盘空间啊喂0.0,所以如果C盘空间不充足,建议修改。
Suppose my nodejs installation directory is: E:\nodejs

  1. First create two folders node_global node_cache under E:\nodejs

  2. Change the global warehouse and dependent package cache location to the folder we set
    Command line execution:
    a. Set the global warehouse location:
npm config set prefix "E:\nodejs\node_global"

b. Set the default cache location:

npm config set cache "E:\nodejs\node_cache"
  1. Check whether the setting is successful

a. Enter the command line to view the location of the global warehouse:

npm list -global

设置正确,则会显示你更改过后的目录下,且下面列表为 empty
b. Then continue with the command line and install any package globally

npm install express -g

c. Then enter the command line to view the location of the global warehouse and cached packages:

npm list -global

设置正确,则会显示你更改过后的目录下,且下面列表中有 express 这个包

2. Configure proxy (*)

If you don't need a proxy to surf the Internet, please skip here ==.
If you need to configure a proxy on the Internet, continue with the npm settings:

npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080

If the proxy requires an account password to log in, use:

npm config set proxy http://username:password@yourproxy.com:port
npm config set https-proxy http://username:password@yourproxy.com:port

username: username
password: password
yourproxy: proxy URL
port: proxy port

3. Configure environment variables

We need to configure environment variables for npm and nodejs.

On the computer icon, right-click -> Properties -> Advanced System Configuration -> Environment Path, you can see the configuration interface of the environment path.

System variable:
new, variable name: NODE_PATH, variable value: E:\nodejs\node_modules\
此时配置好系统变量中就多了NODE_PATH这一栏。

User variables:
We need to modify the default module call path of nodejs, that is, the global warehouse address we configured, (we changed the original C drive to our installation location).
Select Path, edit, then select the npm one, and modify it to: E:\nodejs\node_global\

Then click OK to save the configuration.

Three, yarn installation configuration

Execute from the command line to install;

npm install -g yarn

At this time, it is no problem to use it in the editor. If you want to use it in cmd, you need to configure the environment variable;

4. Notes on installation problems

  1. The yarn command is used in vscode, and an unknown command is reported:
    Reason: The integrated terminal in vscode uses powershell, so we need to set the execution permission of powershell.
    Solution: Enter the C:\Windows\System32\WindowsPowerShell\v1.0 directory and find the powershell.exe file.
    Right-click the file and execute it with administrator privileges. Execute set-ExecutionPolicy RemoteSignedthe command, select y, and restart to take effect.
  2. 407 error, the authority
    does not have permission, check the proxy configuration.
  3. Error npm ERR! cb() never called!:
    Reason: Npm’s own caching mechanism problem;
    solution: 1) Delete the package-lock.json file and node_modules folder in the project, and then re-npm install. (It’s good to reinstall Dafa)
    2) Or forcibly clear the cachenpm cache clean -f
  4. Many problems, such as stuck, download failure, you can try to switch the download source.
    We may use npm in many ways:
    1) Local machine, use proxy, connect to Taobao mirror source
    2) Local machine, use proxy, connect to npm source
    3) Local machine, connect to Taobao mirror source
    4) Local machine, connect to npm source
    You You can verify whether there is a problem with each step according to the way you connect;

Guess you like

Origin blog.csdn.net/qq_37291367/article/details/124691027