Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing global npm packages when using nvm

Tags:

node.js

npm

I feel like I need to be able to install global npm packages separately into the NVM dir.

$ nvm install v0.11
######################################################################## 100.0%
Now using node v0.11.16
$ node-inspector
Node Inspector v0.9.2
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.

my node-inspector stopped working!

$ npm update -g
<bunch of updating, succeeds>

Maybe this will help... Nope! still broken. Sigh. Perhaps... node-inspector doesn't work for a v0.11.16 (that's being run on nvm). Perhaps. Who knows. Well, time to abort.

$ nvm use system
Now using system version of node: v0.10.32.
$ node-inspector --version
dyld: lazy symbol binding failed: Symbol not found: _node_module_register
  Referenced from: /usr/local/lib/node_modules/node-inspector/node_modules/ws/build/Release/bufferutil.node
  Expected in: dynamic lookup

dyld: Symbol not found: _node_module_register
  Referenced from: /usr/local/lib/node_modules/node-inspector/node_modules/ws/build/Release/bufferutil.node
  Expected in: dynamic lookup

[1]    93845 trace trap  node-inspector --version

Okay.... Well.... Shit.

So anyway, at this point I'm not looking for instructions on how to fix. I may well be hosed. The main question here is what am I supposed to do to manage these npm packages which are meant to be command line tools and which have compiled components that appear tied to specific versions? I understand that when I install nvm and incorporate it into my system, my shell is able to switch its $PATH so that when I call node and npm, they will run using the node version I picked.

But it appears as though the global npm packages get put into /usr/local/bin/ somewhere and they're just stuck there and become unable to follow what I do with nvm -- While it may not be the case for the "theoretically well-behaved node package", in practice (for something like node-inspector at least) it sure looks like it's liable to gleefully explode when not executed using the node that installed it.

In the meantime I have to basically npm remove -g <package> && npm install -g <package> any package that I find behaves strangely in this manner, every single time that I want to run that package under a new node version using nvm.

This seems wrong.

Is it wrong?

And, a corollary to this would be that every time I invoke nvm powers to test some given node.js app with a different version of node, I pretty much should do it by cloning it in a whole new directory and starting fresh, because otherwise I'll probably find out that I will need to rm -rf node_modules && npm install just to make it function at all...

like image 221
Steven Lu Avatar asked Mar 13 '15 02:03

Steven Lu


1 Answers

UPDATE October 2020:

If you've already installed the desired Node version, according to the documentation this is also available:

nvm reinstall-packages <from-version>

Props to @rashi for pointing out the syntax above.


From the nvm help message:

nvm install [-s] <version>                Download and install a <version>, [-s] from source. Uses .nvmrc if available
    --reinstall-packages-from=<version>     When installing, reinstall packages installed in <node|iojs|node version number>
    --lts                                   When installing, only select from LTS (long-term support) versions
    --lts=<LTS name>                        When installing, only select from versions for a specific LTS line

The relevant flag is --reinstall-packages-from=<version>. Simply re-run your command (example):

$ nvm install v6.9.2 --reinstall-packages-from=v4.4.5
like image 166
seangates Avatar answered Sep 29 '22 10:09

seangates