使用 nvm 时管理全局 npm 包 [英] Managing global npm packages when using nvm

查看:87
本文介绍了使用 nvm 时管理全局 npm 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我需要能够将全局 npm 包单独安装到 NVM 目录中.

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>

也许这会有所帮助...不!还是坏了.叹.也许……节点检查器不适用于 v0.11.16(在 nvm 上运行).也许.谁知道.嗯,是时候放弃了.

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.

所以无论如何,在这一点上,我不是在寻找有关如何修复的说明.我很可能被灌输了.这里的主要问题是我应该怎么做来管理这些 npm 包,这些包是命令行工具,并且编译了与特定版本相关的组件?我知道当我安装 nvm 并将其合并到我的系统中时,我的 shell 能够切换它的 $PATH 以便当我调用 nodenpm,它们将使用我选择的节点版本运行.

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.

但似乎全局 npm 包被放入 /usr/local/bin/ 某处,它们只是卡在那里,无法按照我对 nvm 所做的操作 -- 虽然理论上表现良好的节点包"可能不是这种情况,但实际上(至少对于 node-inspector 之类的东西)它确实看起来很容易不使用安装它的节点执行时,兴高采烈地爆炸.

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.

与此同时,我必须基本上 npm remove -g <package>&&npm install -g <package> 我发现的任何包都以这种方式表现得很奇怪,每次我想使用 nvm 在新节点版本下运行该包时.

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.

这似乎是错误的.

有错吗?

而且,一个推论是,每次我调用 nvm powers 以使用不同版本的 node 测试某些给定的 node.js 应用程序时,我几乎应该通过将它克隆到一个全新的目录并重新开始来完成,否则我可能会发现我需要 rm -rf node_modules &&npm install 只是为了让它发挥作用...

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...

推荐答案

2020 年 10 月更新:

如果您已经安装了所需的 Node 版本,根据文档,这也可用:

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

nvm reinstall-packages <from-version>

提示 @rashi 指出上述语法.

Props to @rashi for pointing out the syntax above.

来自 nvm 帮助消息:

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

相关标志是 --reinstall-packages-from=.只需重新运行您的命令(示例):

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

这篇关于使用 nvm 时管理全局 npm 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆