node/npm 安装的版本并非使用版本

本文介绍了当更新Node.js及npm版本后仍显示旧版本的原因,并提供了通过设置软链接来解决问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现象

npm install n
n stable
获取最新版本的node 之后,node –version /npm –version 仍然是原来的低版本.

产生原因:

which node/ which npm

可以看到当前正在使用的node/ npm 版本的目录

/usr/bin/npm

我们在全局环境中使用Node/npm 时,会自动去这个目录下寻找node/npm .当我们新安装版本的时候,安装的地方却不是这里,因此,即便安装了最新版本,使用的时候,环境并不能正确知道,只找到了原来的版本.

这就是为什么明明安装了最新版本的node/npm 却无法使用.

解决办法:

把/usr/bin/npm 看成是一个指针.将这个指针指向我们新安装的位置即可.即,建立正确的软链接.
ll which node
获取 最新node的位置: /usr/local/n/vsersions/node/
ll which npm
获取最新npm 的位置:/usr/local/n/versions/node/10.6.0/bin/npm
sudo rm /usr/bin/npm
ln -s /usr/local/n/versions/node/10.6.0/bin/npm /usr/bin/npm

此时,再次检测使用的npm 版本 npm –version ,可发现已经是最新版本.

0 info it worked if it ends with ok 1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'serve' ] 2 info using npm@6.14.18 3 info using node@v22.13.0 4 verbose run-script [ 'preserve', 'serve', 'postserve' ] 5 info lifecycle agent-bar-demo@0.1.0~preserve: agent-bar-demo@0.1.0 6 info lifecycle agent-bar-demo@0.1.0~serve: agent-bar-demo@0.1.0 7 verbose lifecycle agent-bar-demo@0.1.0~serve: unsafe-perm in lifecycle true 8 verbose lifecycle agent-bar-demo@0.1.0~serve: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/hyj/Downloads/webrtc-webphone-master/node_modules/.bin:/Library/Frameworks/Python.framework/Versions/3.12/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin://Applications/Topaz Photo AI.app/Contents/Resources/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/hyj/Downloads/webrtc-webphone-master/node_modules/.bin 9 verbose lifecycle agent-bar-demo@0.1.0~serve: CWD: /Users/hyj/Downloads/webrtc-webphone-master 10 silly lifecycle agent-bar-demo@0.1.0~serve: Args: [ '-c', 'vue-cli-service serve' ] 11 silly lifecycle agent-bar-demo@0.1.0~serve: Returned: code: 1 signal: null 12 info lifecycle agent-bar-demo@0.1.0~serve: Failed to exec serve script 13 verbose stack Error: agent-bar-demo@0.1.0 serve: `vue-cli-service serve` 13 verbose stack Exit status 1 13 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16) 13 verbose stack at EventEmitter.emit (node:events:524:28) 13 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14) 13 verbose stack at ChildProcess.emit (node:events:524:28) 13 verbose stack at maybeClose (node:internal/child_process:1101:16) 13 verbose stack at ChildProcess._handle.onexit (node:internal/child_process:304:5) 14 verbose pkgid agent-bar-demo@0.1.0 15 verbose cwd /Users/hyj/Downloads/webrtc-webphone-master 16 verbose Darwin 22.6.0 17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "serve" 18 verbose node v22.13.0 19 verbose npm v6.14.18 20 error code ELIFECYCLE 21 error errno 1 22 error agent-bar-demo@0.1.0 serve: `vue-cli-service serve` 22 error Exit status 1 23 error Failed at the agent-bar-demo@0.1.0 serve script. 23 error This is probably not a problem with npm. There is likely additional logging output above.
03-18
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值