After nvm switches the node version, the global node modules does not change, the solution

Hits: 0

I only installed http-server once, but no matter how I switch the [node] version, it can be used, but when I use it on a mac, after I install it globally according to the default version, other versions cannot be used.

Reason: The default and folders of node_cachenpm are built in the directory, so when you use this global installation, it will be automatically installed in the directory , but not installed in the version corresponding to the node under the nvm directory, so it is installed globally. Module, which will not change with node version switching. npm global installation path viewnode_global:\Program Files\nodejs install -g http-server:\Program Files\nodejs\node_global
npm config ls

Solution: When switching node versions, node_cachechange node_globalthe paths of npm and

Solution:
Method 1:
Directly install normally, and then copy the corresponding files in C:\Program Files\nodejs\node_globalthe directory to the nvm directory, under the node folder of the corresponding version, for example, here I am D:\ProgramFiles\nvm\v12.12.0. Then copy the corresponding node_modulesfolder directly, and then C:\Program Files\nodejs\node_globaldelete the file just copied in the directory
Original global installation folder:
nvm’s node corresponding version folder:

Method 2:
When switching node versions, replace the global installation directory of npm

npm config set cache "D:\ProgramFiles\nvm\v version number" 
# For example: npm config set prefix "D:\ProgramFiles\nvm\v12.12.0"

npm config set prefix "D:\ProgramFiles\nvm\vversion\node_cache" 
# Example: npm config set cache "D:\ProgramFiles\nvm\v12.12.0\node_cache"

Then do a npm install -g http-serverglobal install

Switch version:
Install global module:
Use test:
The above method, the globally installed module can be changed with the node version change.

If the module is compatible with the node version you need, you can install it directly without modifying the npm global installation path. If the module is distinguished by node version, install it as above

When uninstalling and installing global modules, remember to add them -g, otherwise, errors will occur.
Code to restore the original directory of the npm install:

npm config set prefix "C:\Program Files\nodejs\node_global"
npm config set cache "C:\Program Files\nodejs\node_cache"

It is not easy to create, but watch and cherish, please explain ~!

You may also like...

Leave a Reply

Your email address will not be published.