首页 > 解决方案 > “npm help update”(在 Windows 中)给出无效 URL 错误

问题描述

我有 nodejs v16.13.0 和 npm 8.1.0。

当我npm help update在 cmd 或 PowerShell(管理员和普通)中运行命令时,出现以下错误

npm help update
npm ERR! Invalid URL: file://C:%5CProgram%20Files%5Cnodejs%5Cnode_modules%5Cnpm%5Cdocs%5Coutput%5Ccommands%5Cnpm-update.html

我可以确认该文件存在于该位置,但由于某种原因,该命令未正确读取路径。是否有我需要更改的文件?

我还没有在网上找到任何相关的解决方案......到目前为止,这里也没有。

标签: node.jsurlnpm

解决方案


我对所有 npm help ... 命令都有同样的问题,例如:

C:\DEV\youtube>npm help npm
npm ERR! Invalid URL: file://C:%5CUsers%5Cmirek%5CAppData%5CRoaming%5Cnpm%5Cnode_modules%5Cnpm%5Cdocs%5Coutput%5Ccommands%5Cnpm.html

npm ERR! A complete log of this run file:///C:/Users/mirek/AppData/Roaming/npm/node_modules/npm/docs/output/commands/npm.htmlcan be found in:
npm ERR!     C:\Users\mirek\AppData\Local\npm-cache\_logs\2021-11-04T06_20_51_613Z-debug.log

似乎存在编码 URL 的问题,因为文件本身存在。如果 URL 解码成

file:///C:/Users/mirek/AppData/Roaming/npm/node_modules/npm/docs/output/commands/npm.html

它可以在浏览器中打开,它工作得很好。另外,根据登录

C:\Users\mirek\AppData\Local\npm-cache\_logs\2021-11-04T06_20_51_613Z-debug.log

问题出在

C:\Users\mirek\AppData\Roaming\npm\node_modules\npm\lib\utils\open-url.js:31:11.

它试图验证文件 URL 是否正确。但是,它显然存在编码 URL 的问题。

因此,我尝试在该文件中注释第 7 行,该文件对 URL 进行编码:

5.    // attempt to open URL in web-browser, print address otherwise:
6:    const open = async (npm, url, errMsg) => {
7:      //url = encodeURI(url)
8:      const browser = npm.config.get('browser')

修改后它工作得很好。


推荐阅读