Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgraded node and npm via nvm, but old node is still used for global packages

I've been using node 9.3.0 for a long time, but I recently migrated to 10.12.0. Everything went fine, when I do node -v and npm -v I get the correct versions:

Paul-Bergs-Macbook:node paulrberg$ node -v
v10.12.0
Paul-Bergs-Macbook:node paulrberg$ which node
/Users/paulrberg/.nvm/versions/node/v10.12.0/bin/node
Paul-Bergs-Macbook:node paulrberg$ npm -v
6.4.1
Paul-Bergs-Macbook:node paulrberg$ which npm
/Users/paulrberg/.nvm/versions/node/v10.12.0/bin/npm

Howeven, when I'm trying to run any npm command, the old version is used. That is:

Paul-Bergs-Macbook:node paulrberg$ npm i truffle -g
/Users/paulrberg/.nvm/versions/node/v9.3.0/bin/truffle -> /Users/paulrberg/.nvm/versions/node/v9.3.0/lib/node_modules/truffle/build/cli.bundled.js
+ [email protected]
added 81 packages from 311 contributors in 1.715s

And:

npm list -g --depth=0
/Users/paulrberg/.nvm/versions/node/v9.3.0/lib
└── [email protected]

Not sure if this is some bash code still pointing to the last version, but I can't seem to find any proof for that. Running env and checking for 9.3.0 environment variables yields no result.

What I did so far:

  • Delete node 9.3.0 with nvm uninstall 9.3.0
  • Do a fresh install of nvm after deleting it and rebooting the computer
  • nvm reinstall-with-packages
  • Deleted ~/.nvmrc and set 10.12.0 in there afterwards
  • Check if I have an overlapping node from homebrew and I don't What could the problem be?
like image 657
Paul Razvan Berg Avatar asked Oct 21 '18 11:10

Paul Razvan Berg


2 Answers

After a few hours of painful Unix debugging, I realised the problem was that I set a prefix in npm config:

prefix = "/Users/paulrberg/.nvm/versions/node/v9.3.0"

To fix this, make sure to unset the prefix by doing:

npm config rm prefix

This is mentioned, albeit not necessarily shining in the nvm doc

If you have an ~/.npmrc file, make sure it does not contain any prefix settings (which is not compatible with nvm)

like image 64
Paul Razvan Berg Avatar answered Oct 25 '22 16:10

Paul Razvan Berg


It looks like you might need to run nvm reinstall-packages

https://github.com/creationix/nvm#migrating-global-packages-while-installing

which says


Migrating global packages while installing If you want to install a new version of Node.js and migrate npm packages from a previous version:

nvm install node --reinstall-packages-from=node

This will first use "nvm version node" to identify the current version you're migrating packages from. Then it resolves the new version to install from the remote server and installs it. Lastly, it runs "nvm reinstall-packages" to reinstall the npm packages from your prior version of Node to the new one.

You can also install and migrate npm packages from specific versions of Node like this:

nvm install 6 --reinstall-packages-from=5 nvm install v4.2 --reinstall-packages-from=iojs


The other "solution" is not to use global packages. Particularly when using nvm and not being able to be sure that the global package is for the "current" version it can be better to install locally and use npx to run the local command

truffle installs a truffle command to ./node_modules/.bin when you npm install it so you can npx truffle to run the local one instead of truffle to run the global one


edit:

another thing to check is that node -v and nvm current don't necessarily report the same version.

I wonder if nvm current would report v9.3 for you?

enter image description here

ah, yep, on my machine I can install truffle globally in a different location than node -v reports

enter image description here

 > node -v
v9.5.0
 > nvm current
system
 > nvm use v8
Now using node v8.4.0 (npm v5.3.0)
 > node -v
v8.4.0
 > nvm current
v8.4.0
 > npm install -g truffle
/Users/pauldambra/.nvm/versions/node/v8.4.0/bin/truffle -> /Users/pauldambra/.nvm/versions/node/v8.4.0/lib/node_modules/truffle/build/cli.bundled.js
+ [email protected]
added 81 packages in 4.364s

So you might be missing an nvm use v10 command

like image 39
Paul D'Ambra Avatar answered Oct 25 '22 16:10

Paul D'Ambra