Writing in the front

For programmers who write JS, NPM is probably not unknown, but some students are not familiar with its configuration file (i.e..npMRc file). Based on my learning experience, I will write a blog post to share some knowledge about this configuration file.

NPMRC role

.npMRc (NPM running CNfiguration) We know that NPM’s biggest role is to help developers install the dependencies they need, but where to download them? Which version of the package to download, and which path to download the package to the computer?

These can all be configured in.npMRc.

Before we set up.npmrc, we need to know that there is not just one.npmrc file on your computer, but multiple files. When we install the package, NPM reads these configuration files in the following order:

  1. Project configuration files: You can create a.npmrc file in the root directory of your project to manage only the NPM installation for that project.
  2. User profile: You can create a.npmrc file for the current user when you log in to the computer with an account, and then use this profile when you log in to the computer with that account. You can obtain the location of the file by NPM config get userconfig.
  3. Global profile: A computer may have multiple users, on top of which you can set up a common.npMRc file for all users to use. The file path is $PREFIX/etc/npmrc. Run NPM config get PREFIX to obtain $PREFIX. If you have not configured the global file, the file does not exist.
  4. NPM built-in configuration files: Finally, there are NPM built-in configuration files, which are rarely needed, so don’t worry too much about them.

How do I set.npMRc

1. Set up the project profile

Create a.npmrc file in the root directory of your project and configure it in the key=value format. For example, to configure the NPM source as Taobao source, you can refer to the code:

registry=https://registry.npm.taobao.org
Copy the code

If you want to delete some configuration, you can simply delete the corresponding line of code.

2. Set up the user profile

You can run the NPM config get userconfig command directly to find the path of the file, and then directly copy the above method of the file, or you can run the NPM config set command to continue to set the file:

config set registry https://registry.npm.taobao.org
Copy the code

Finally, the command line will help us modify the corresponding configuration file. It’s just faster to use the command line.

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

npm config delete registry
Copy the code

3. Set up the global configuration file

The method is the same as setting up the user profile, except that the -g argument is used when using the command line.

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

In addition, here is a list of some common NPM setup commands, if you are interested, it is fun to learn about them:

npm config set< key > < value > [| -- - g global] / / to the setting of the configuration parameters of the key value; NPM config get <key> // Obtain the key value. NPM config delete <key> NPM config list [-l] NPM config list [-l] NPM config edit // Edit the configuration file NPM get <key> // Obtain the value of the configuration parameter key; npmset< key > < value > [| -- - g global] / / to the setting of the configuration parameters of the key value;Copy the code

Write in the last

So that’s a little bit of common sense about.nPMRc, which you will rarely continue to configure during development. However, when you install a dependency package error, you can think about: is there a problem with the configuration parameters of NPM, so there is a way to solve the problem.

Like this blog post:

Solution to the node-sass installation failure: juejin.cn/post/698216…