Node.js module global installation path configuration

By default, the NodeJS installation will also install npm (module manager: modules used to manage user requirements, both global and local).

Note:

Global: Execute npm install <module name> -g to install the module in the global path. When the user requires (<module name>) in the program, it does not need to consider where the module is. If the global path is not modified, the user The downloaded module will default to C:\Users\Administrator\AppData\Roaming\npm.

Partially: executing npm install <package name> (note that -g is missing) will install the module in the path currently pointed to by the dos window. At this time, other path items cannot reference the module of this version!

Modify the default global path:

First configure the storage path of npm's global module (node_global) and the path of the cache (global_cache). For example, I want to put the above two folders under %nodejs%\node_modules

1) Create two folders "node_global" and "node_cache" under D:\apps\nodejs\node_modules\.

 

2) Execute in the DOS window:

npm config set prefix "D:\myFiles\javascript\repository\node_global"

npm config set cache "D:\myFiles\javascript\repository\node_cache"

If this method cannot be modified, it is OK:

Find the node_modules\npm\.npmrc file in the nodejs installation directory

Modify as follows:

prefix = D:\apps\nodejs\node_modules\node_global
cache = D:\apps\nodejs\node_modules\node_cache

3) At this point, the default global path of npm module installation has been changed to the corresponding folder, but  at this time the user still cannot require these modules, because the computer system does not know that you have changed the default path, so it needs to be in " win+R"-->"sysdm.cpl"-->"Advanced"-->"Environment Variables" to open the settings dialog.

 

4) The following is the most critical place (the place that decides the success or failure of the modification!):

① First, create a new variable in "System Variables", create a new variable named NODE_PATH, and the variable value is: the absolute path of the node_modules folder in the previously created folder for storing global modules. (Tell system  users that downloaded global modules are here).

即:D:\apps\nodejs\node_modules\node_global\node_modules

②After creating NODE_PATH, you also need to tell the system the location of node and npm. At this time, you need to find the "Path" variable in the "System Variables" column of the "Environment Variables" dialog box.

 

Double-click to open, add an English semicolon ";" at the end of the existing content, and then check whether there is any

D:\apps\nodejs\;         (address of node and npm)

The following one needs to be added if cnpm is installed:

D:\apps\nodejs\node_modules\node_global;

       (cnpm路径,npm install -g cnpm --registry=https://registry.npm.taobao.org!!!)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326025322&siteId=291194637