npm configuration file - npmrc

Reprinted to: npm configuration file-npmrc_npmrc configuration_sponge full_wk's blog-CSDN blog

1. The role of the .npmrc configuration file
.npmrc can be understood as npm running cnfiguration, that is, the npm runtime configuration file. To put it simply, .npmrc can set the installation source of dependent packages in package.json, that is, where to download dependent packages.

2. The priority of .npmrc configuration files
There are multiple .npmrc files in the computer. When we install the package, npm reads these configuration files in the following order

Project configuration file: /project/.npmrc
User configuration file: ~/.npmrc
Global configuration file: $PREFIX/etc/npmrc
npm built-in configuration file /path/to/npm/npmrc

 
 
  1. # 获取 .npmrc 用户配置文件路径

  2. npm config get userconfig

  3. // 如果想恢复默认配置,只需要将用户配置文件~/.npmrc删除即可;

 
 
  1. # 获取 .npmrc 全局配置文件路径 $PREFIX

  2. npm config get prefix

  3. // 如果你不曾配置过全局文件,该文件不存在。

 
 
  1. # 获取 npmrc 在npm的路径

  2. which npm

  3. // 与npm同级别, 使用which npm获取 npm的路径



3. How to set .npmrc
1. Set the project configuration file
The .npmrc file under the project has the highest priority. You can configure different mirrors for each project, and the configurations between projects do not affect each other.

Create a new .npmrc file in the root directory of the project, and configure it in the format of key=value.

registry=https://registry.npm.taobao.org


It is also possible to specify the source of a special namespace (scope).

Packages starting with @test are downloaded from registry=https://npm.xx.com, and all others are downloaded from Taobao mirror .

 
 
  1. registry=https://registry.npm.taobao.org/

  2. @test:registry = https://npm.xx.com


2. Set the user configuration file
You can directly find the path of the file through the npm config get userconfig command, and then directly follow the above method to set the file, or you can continue to set it through the npm config set command

npm config set registry https://registry.npm.taobao.org


If you want to delete some configurations, you can directly edit the .npmrc file, or use commands to delete, such as:

npm config delete registry


3. Setting the global configuration file
The method is the same as setting the user configuration file, except that the -g parameter needs to be added when using the command line.

npm config set registry https://registry.npm.taobao.org -g


4. Add the registration source option to the npm command
to temporarily use the Taobao mirror

npm --registry=https://registry.npm.taobao.org [npm命令]


For example, use npm to install dependencies

npm install  --registry https://registry.npmjs.org


This approach is not recommended, and it is more recommended to set the project configuration file.npmrc

Fourth, npm common commands
 

 
 
  1. npm config set <key> <value> [-g|--global]  //给配置参数key设置值为value;

  2. npm config get <key>                        //获取配置参数key的值;

  3. npm config delete <key>  [-g|--global]      //删除置参数key及其值;

  4. npm config list [-l]                        //显示npm的所有配置参数的信息;

  5. npm config edit                             //编辑用户配置文件

  6. npm get <key>                               //获取配置参数 key 生效的值;

  7. npm set <key> <value> [-g|--global]         //给配置参数key设置值为value;


Five, yarn
yarn will read the .npmrc configuration file, so there is no need to set it again for yarn

6. Problems
1. The mirror error
. The npmrc file is configured with a private package registry source, but the current download source is a Taobao mirror, and an error may be reported

error Command failed with exit code 1

You can change the current download source to a private package registry source. If you still get an error after changing it, you can try to delete the lock file or node_modules file and restart the download.

Guess you like

Origin blog.csdn.net/weixin_46016659/article/details/129816638