npm install, npm ERR code 401 Incorrect or missing password error reason and use of .npmrc configuration file

Foreword: When the front-end maintains the project, after git clone, it is often directly npm install to install the node_modules required by the project, but often many projects are not written by ourselves, or cloned from GitHub. Open source projects, when problems arise at this time It is difficult to deal with, share the troubleshooting plan, so that you know what is happening and why!

 

 1. The .npmrc file

  Let's talk about the configuration file of npmrc first, so that we can troubleshoot the problem below!

 

  1. The role of npmrc

   .npmrc can be understood as npm running configuration, the configuration file for npm runtime. We know that the biggest role of npm is to help developers install the required dependency packages, but where do we download them from? Which version of the package to download, and which path should the package be downloaded to? Is the path of the private package or the path of the public package downloaded? I have a private package, how to configure the path?

 

All of the above can be configured in .npmrc

Before setting up .npmrc, we need to know: on your computer, there is not just one .npmrc file, but multiple.

When we install the npm package, npm reads these configuration files in the following order:

  1. Project configuration file : It is the .npmrc file seen in the screenshot above, which is only used to manage the npm installation of the current project;
  2. User configuration file : When we log in to the computer with an account, we can create a .npmrc file for the current user, and then log in with this account, then we can use this configuration file to specify the source to download and use this configuration file. The file location can be obtained through npm config get userconfig;
  3. Global configuration file : A computer may have multiple users. On top of these users, a common .npmrc file can be set for all users to use. The path of this file is: $PREFIX/etc/npmrc , use  npm config get prefix  to get $PREFIX. If you have never configured a global file, this file does not exist.
  4. Npm built-in configuration file : The built-in configuration file of npm is basically not used by individuals, so there is no need to pay too much attention; 

  2. Set up the project configuration file

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

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

 

  3. Set up user profiles

     You can directly find the path of the file through  the npm config get userconfig  command, and then follow the above method directly to the file, or you can  continue to set it through the npm config set  command, the command is as follows:

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

 

  4. Set the global configuration file

The method is exactly 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

 The above is some common sense about .npmrc. In the development process, this file is rarely configured, but I will talk about one next! However, when we make mistakes in installing dependencies, we can have one more idea, so as not to be confused!


 

2. npm ERR code 401, error analysis

  1. The installation is a private package, and npm needs to be logged in before it can be installed

npm ERR! code E401
npm ERR! Incorrect or missing password.
npm ERR! If you were trying to login, change your password, create an
npm ERR! authentication token or enable two-factor authentication then
npm ERR! that means you likely typed your password in incorrectly.
npm ERR! Please try again, or recover your password at:
npm ERR!     https://www.npmjs.com/forgot
npm ERR! 
npm ERR! If you were doing some other operation then your saved credentials are   
npm ERR! probably out of date. To correct this please try logging in again with:  
npm ERR!     npm login

npm ERR! A complete log of this run can be found in:
npm ERR!     D:\other\nodejs\node_cache\_logs\2022-10-15T13_22_01_878Z-debug-0.log

 You can see the picture above, there is a .npmrc project configuration file in my project, so when I execute npm install, it will read my current specified download location by default, because my project needs to download private node_modules, so configure this path;

Error reason: private package requires user permission

Solution: npm login is fine, log in to the authorized npm account

 

2. npm ERR! Unable to authenticate, need Basic realm ="aliyun"

 However, the error prompt at this time is different from the above, pay attention to realm = "aliyun (Aliyun)"

npm ERR! code E401
npm ERR! Unable to authenticate, need Basic realm ="aliyun"

   

 If you are downloading a private package, you must first check whether you are logged in

  ① Check whether the login account has permission to download private packages. Generally, private packages will provide npm accounts and passwords;

  ② Instead of downloading a private package and reporting an error, switch the project register address to the npm.js.org official account

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

3. If you want to publish your own npm package on npm, execute npm login and report an error   

PS D:\Workspace\WebstormProjects\lhqm-ngzorro>  npm login
Username: qianmian
Password:
Email: (this IS public) [email protected]
npm ERR! code E401
npm ERR! Incorrect or missing password.
npm ERR! If you were trying to login, change your password, create an
npm ERR! authentication token or enable two-factor authentication then
npm ERR! that means you likely typed your password in incorrectly.
npm ERR! Please try again, or recover your password at:
npm ERR!     https://www.npmjs.com/forgot
npm ERR!
npm ERR! If you were doing some other operation then your saved credentials are
npm ERR! probably out of date. To correct this please try logging in again with:
npm ERR!     npm login

npm ERR! A complete log of this run can be found in:
npm ERR!     F:\Program_Files\QianDuan\nodejs\node_cache\_logs\2021-04-04T14_44_24_023Z-debug.log

Reason for the error: You need to check the current npm download path, as mentioned above; the Taobao mirror only supports downloading, not uploading and publishing;

Solution: Re-point the configured register to the npm official;

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

  4. npm ERR! code ETARGET

  notarget No matching version found for uxcore@^1.0.23. From this sentence, we can roughly understand that the 1.0.23 version of the uxcore package is not found in the current npm configuration file download source;

  Why does this happen?

  We first need to confirm whether the package is public or privatized . There is a high probability that it is a privatized package. When we do MultipleRepo project management, we often use privatized npm packages to manage inter-project dependencies!

  To determine the above reasons, we can set the corresponding npm registery in the project configuration file -> .npmrc, and execute npm install again

PS E:\uxretail-ui> npm install
npm ERR! code ETARGET
npm ERR! notarget No matching version found for uxcore@^1.0.23.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.

npm ERR! A complete log of this run can be found in:
npm ERR!     D:\other\nodejs\node_cache\_logs\2022-10-15T14_40_12_462Z-debug-0.log

Guess you like

Origin blog.csdn.net/weixin_56650035/article/details/127341087