You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级npm至最新版本时遇两类错误的解决方法咨询

问题描述

我想安装最新版本的npm,执行了命令:

npm install -g npm@latest

但出现错误:

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: strip-ansi-cjs@npm:strip-ansi@^6.0.1 (node_modules\npm\node_modules\@isaacs\cliui\node_modules\strip-ansi-cjs):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Unsupported URL Type "npm:": npm:strip-ansi@^6.0.1
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: wrap-ansi-cjs@npm:wrap-ansi@^7.0.0 (node_modules\npm\node_modules\@isaacs\cliui\node_modules\wrap-ansi-cjs):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: Unsupported URL Type "npm:": npm:wrap-ansi@^7.0.0

npm ERR! code EUNSUPPORTEDPROTOCOL
npm ERR! Unsupported URL Type "npm:": npm:string-width@^4.2.0

npm ERR! A complete log of this run can be found in:

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Users\\d_quang\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'install',
1 verbose cli   '-g',
1 verbose cli   'npm@latest' ]
2 info using npm@5.2.0
3 info using node@v8.9.1
4 verbose npm-session 3a978f7eb7e14fef
5 silly install loadCurrentTree
6 silly install readGlobalPackageData
7 http fetch GET 200 https://registry.npmjs.org/npm 22ms (from cache)
8 silly pacote tag manifest for npm@latest fetched in 69ms
9 silly install loadIdealTree
10 silly install cloneCurrentTreeToIdealTree
11 silly install loadShrinkwrap
12 silly install loadAllDepsIntoIdealTree
13 silly resolveWithNewModule npm@10.2.4 checking installable status
14 silly pacote trying https://registry.npmjs.org/npm/-/npm-10.2.4.tgz by hash: sha512-umEuYneVEYO9KoEEI8n2sSGmNQeqco/3BSeacRlqIkCzw4E7XGtYSWMeJobxzr6hZ2n9cM+u5TsMTcC5bAgoWA==
15 silly pacote https://registry.npmjs.org/npm/-/npm-10.2.4.tgz extracted to C:\Users\d_quang\AppData\Local\Temp\npm-12960-11425905\unpack-0dbf2b1c by content address 2290ms
16 silly addBundled read tarball
17 silly cleanup remove extracted module
18 warn optional SKIPPING OPTIONAL DEPENDENCY: strip-ansi-cjs@npm:strip-ansi@^6.0.1 (node_modules\npm\node_modules\@isaacs\cliui\node_modules\strip-ansi-cjs):
19 warn optional SKIPPING OPTIONAL DEPENDENCY: Unsupported URL Type "npm:": npm:strip-ansi@^6.0.1
20 warn optional SKIPPING OPTIONAL DEPENDENCY: wrap-ansi-cjs@npm:wrap-ansi@^7.0.0 (node_modules\npm\node_modules\@isaacs\cliui\node_modules\wrap-ansi-cjs):
21 warn optional SKIPPING OPTIONAL DEPENDENCY: Unsupported URL Type "npm:": npm:wrap-ansi@^7.0.0
22 verbose stack Error: Unsupported URL Type "npm:": npm:string-width@^4.2.0
22 verbose stack     at unsupportedURLType (C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\node_modules\npm-package-arg\pa.js:190:15)
22 verbose stack     at fromURL (C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\node_modules\npm-package-arg\pa.js:243:13)
22 verbose stack     at Function.resolve (C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\node_modules\npm-package-arg\pa.js:62:12)
22 verbose stack     at childDependencySpecifier (C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\lib\install\deps.js:80:14)
22 verbose stack     at addDependency (C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\lib\install\deps.js:518:15)
22 verbose stack     at C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\lib\install\deps.js:468:5
22 verbose stack     at C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\node_modules\slide\lib\async-map.js:52:35
22 verbose stack     at Array.forEach (<anonymous>)
22 verbose stack     at C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\node_modules\slide\lib\async-map.js:52:11
22 verbose stack     at Array.forEach (<anonymous>)
22 verbose stack     at asyncMap (C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\node_modules\slide\lib\async-map.js:51:8)
22 verbose stack     at loadDeps (C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\lib\install\deps.js:460:3)
22 verbose stack     at Array.<anonymous> (C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\node_modules\slide\lib\bind-actor.js:15:8)
22 verbose stack     at LOOP (C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\node_modules\slide\lib\chain.js:15:14)
22 verbose stack     at chain (C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\node_modules\slide\lib\chain.js:20:5)
22 verbose stack     at C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\lib\install\deps.js:355:5
23 verbose cwd C:\Users\d_quang\Downloads
24 verbose Windows_NT 10.0.19044
25 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\d_quang\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "install" "-g" "npm@latest"
26 verbose node v8.9.1
27 verbose npm  v5.2.0
28 error code EUNSUPPORTEDPROTOCOL
29 error Unsupported URL Type "npm:": npm:string-width@^4.2.0
30 verbose exit [ 1, true ]

切换到Node 20.10.0后(执行nvm use 20.10.0),再次运行同一命令,又出现错误:

npm ERR! cb.apply is not a function

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\d_quang\AppData\Roaming\npm-cache\_logs\2023-11-23T02_48_28_227Z-debug.log

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Users\\d_quang\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'install',
1 verbose cli   '-g',
1 verbose cli   'npm@latest'
1 verbose cli ]
2 info using npm@5.2.0
3 info using node@v20.10.0
4 verbose npm-session 4b07ba4c6a31d26a
5 silly install loadCurrentTree
6 silly install readGlobalPackageData
7 http fetch GET 304 https://registry.npmjs.org/npm 327ms (from cache)
8 silly pacote tag manifest for npm@latest fetched in 379ms
9 verbose stack TypeError: cb.apply is not a function
9 verbose stack     at C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm\node_modules\graceful-fs\polyfills.js:287:18
9 verbose stack     at FSReqCallback.oncomplete (node:fs:200:5)
10 verbose cwd C:\Users\d_quang\Downloads
11 verbose Windows_NT 10.0.19044
12 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\d_quang\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "install" "-g" "npm@latest"
13 verbose node v20.10.0
14 verbose npm  v5.2.0
15 error cb.apply is not a function
16 verbose exit [ 1, true ]

请问该如何修复这些错误?

解决方案

核心原因

你的npm版本(5.2.0)过于老旧,不支持现代npm包使用的npm:协议依赖;同时旧版npm的graceful-fs模块和高版本Node(20.x)存在兼容性冲突。

修复步骤

  1. 彻底清理旧版npm残留

    • 删除全局npm目录:
      rmdir /s /q C:\Users\d_quang\AppData\Roaming\npm\node_modules\npm
      
    • 强制清理npm缓存:
      npm cache clean --force
      
  2. 使用Node内置npm执行更新

    • 切换到Node 20.10.0:
      nvm use 20.10.0
      
    • 直接执行更新命令(此时会调用Node自带的npm,而非残留的旧版):
      npm install -g npm@latest
      
    • 如果仍调用旧版npm,手动指定Node内置npm路径执行:
      "C:\Program Files\nodejs\npm" install -g npm@latest
      
  3. 验证更新结果

    • 关闭当前终端,重新打开后执行:
      npm -v
      
    • 输出为10.x及以上版本,说明更新成功。

备选方案(若上述方法无效)

  • 通过nvm直接安装包含最新npm的Node版本:
    nvm install --latest-npm 20.10.0
    
  • 重新安装nvm,确保环境变量配置正确,避免旧版npm残留干扰。

内容的提问来源于stack exchange,提问作者quangkid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 21:20:56