[英]node-gyp 5 on Windows: Cannot find npm\node_modules\node-gyp\bin\node-gyp.js
Running npm 6.11.3, node-gyp 5.0.3, VS2019 on Windows 10 v1903, getting this error:在 Windows 10 v1903 上运行 npm 6.11.3、node-gyp 5.0.3、VS2019,得到这个错误:
Cannot find module %AppData%\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js
(where %AppData%
is expanded to the actual user-specific path). Cannot find module %AppData%\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js
(其中%AppData%
扩展为实际的用户特定路径)。
The error only happens if the build is invoked via npm, ie:该错误仅在通过 npm 调用构建时发生,即:
npm run build
The package.json
file scripts
section: package.json
文件scripts
部分:
"scripts": {
...
"build": "node-gyp build",
}
If node-gyp build
is invoked manually outside npm context, the error is gone.如果在 npm 上下文之外手动调用
node-gyp build
,错误就会消失。
How do I get rid of the error and still use npm
scripts for making builds?如何摆脱错误并仍然使用
npm
脚本进行构建?
Figured it out.弄清楚了。
After upgrading to node-gyp 5 on a Windows 10 build system, I started getting the infamous Error: Cannot find module ... npm\\node_modules\\node-gyp\\bin\\node-gyp.js
, caused by node_modules\\npm-lifecycle\\node-gyp-bin
being pushed on top of my PATH
by npm, exactly as described here .在 Windows 10 构建系统上升级到 node-gyp 5 后,我开始收到臭名昭著的
Error: Cannot find module ... npm\\node_modules\\node-gyp\\bin\\node-gyp.js
,由node_modules\\npm-lifecycle\\node-gyp-bin
引起node_modules\\npm-lifecycle\\node-gyp-bin
被 npm 推送到我的PATH
之上,完全如这里所述。
None of the existing remedies I've found and tried could solve this issue for me, including installing the latest windows-build-tools
.我找到并尝试过的现有补救措施都不能为我解决这个问题,包括安装最新的
windows-build-tools
。 Here's exactly what I tried, in the following order:这正是我尝试过的,按以下顺序:
PATH
and User PATH
environment varsPATH
和用户PATH
环境变量中的混乱和重复项choco unistall nodejs
rd /s %AppData%\\npm\\
rd /s %AppData%\\npm\\
choco install nodejs
npm install -g npm@latest
npm install -g node-gyp@latest
npm install -g typescript@latest
I was still getting the missing node-gyp.js
error.我仍然收到
missing node-gyp.js
错误。 What did help was to properly set npm config set node_gyp
to point to the correct node-gyp.js
, as suggested here .有什么帮助是正确设置
npm config set node_gyp
以指向正确的node-gyp.js
,如建议here 。 Yet somehow the script from that thread didn't work properly for me (as well some other PowerShell/Batch scripts I tried).然而不知何故,该线程中的脚本对我来说无法正常工作(以及我尝试过的其他一些 PowerShell/Batch 脚本)。
Eventually, I've come up with the following PowerShell 6 one-liner that did the trick for me :最终,我想出了以下 PowerShell 6 one-liner 对我有用的方法:
pwsh -Command npm config set node_gyp "`u{22}$(Join-Path $(npm root -g) -ChildPath 'node-gyp\bin\node-gyp.js')`u{22}"
Hope this helps others and future-me.希望这有助于其他人和未来的我。 I'd still recommend going through the above bullet list for clean troubleshooting.
我仍然建议通过上面的项目符号列表进行干净的故障排除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.