如何修复ReferenceError:节点中未定义primordials [英] How to fix ReferenceError: primordials is not defined in node
问题描述
我已经通过npm install安装了节点模块,然后尝试在命令提示符下执行gulp sass-watch.之后,我得到了以下答复.
I have installed node modules by npm install, then I tried to do gulp sass-watch in command prompt. After that I got the below response.
[18:18:32] Requiring external module babel-register
fs.js:27
const { Math, Object, Reflect } = primordials;
^
ReferenceError: primordials is not defined
在大吃一惊之前尝试过此操作
Have tried this before gulp sass-watch
npm -g install gulp-cli
推荐答案
根据gulp@3.9.1
到Node.js 12更新旧项目时,我们遇到了相同的问题.
We encountered the same issue when updating a legacy project depending on gulp@3.9.1
to Node.js 12.
这些修补程序使您可以通过将graceful-fs
覆盖到版本4.2.3
来将Node.js 12与gulp@3.9.1
一起使用.
These fixes enable you to use Node.js 12 with gulp@3.9.1
by overriding graceful-fs
to version 4.2.3
.
创建一个包含以下内容的 npm-shrinkwrap.json
文件:
Create a npm-shrinkwrap.json
file containing this:
{
"dependencies": {
"graceful-fs": {
"version": "4.2.3"
}
}
}
提交此npm-shrinkwrap.json
文件.然后执行npm install
,这将更新npm-shrinkwrap.json
文件.
Commit this npm-shrinkwrap.json
file. And then execute npm install
which will update the npm-shrinkwrap.json
file.
很遗憾,如果您再次npm install
,此解决方案将无法再使用.请参阅下面的其他解决方案.
Unfortunately, this solution does not work anymore if you npm install
again. See the other solutions below.
Yarn v1 支持将程序包解析为已定义的版本.
您需要在package.json
中添加一个resolutions
部分:
Yarn v1 supports resolving a package to a defined version.
You need to add a resolutions
section to your package.json
:
{
// Your current package.json contents
"resolutions": {
"graceful-fs": "4.2.3"
}
}
感谢 @jazd 这种解决问题的方法.
Thanks @jazd for this way to solve the issue.
使用 npm-force-resolutions
作为预安装脚本,您可以获得类似的结果含纱线v1.您需要通过以下方式修改package.json:
Using npm-force-resolutions
as a preinstall script, you can obtain a similar result as with yarn v1. You need to modify your package.json this way:
{
// Your current package.json
"scripts": {
// Your current package.json scripts
"preinstall": "npx npm-force-resolutions"
},
"resolutions": {
"graceful-fs": "4.2.3"
}
}
npm-force-resolutions
将更改package-lock.json
文件,以在install
完成之前将graceful-fs
设置为所需版本.
npm-force-resolutions
will alter the package-lock.json
file to set graceful-fs
to the wanted version before the install
is done.
如果您在项目中使用自定义.npmrc
文件,并且其中包含代理或自定义注册表,则需要将npx npm-force-resolutions
更改为npx --userconfig .npmrc npm-force-resolutions
,因为到目前为止,npx
尚未使用当前文件夹.npmrc
文件.
If you are using a custom .npmrc
file in your project and it contains either a proxy or custom registry, you need to change npx npm-force-resolutions
to npx --userconfig .npmrc npm-force-resolutions
because as of now, npx
doesn't use the current folder .npmrc
file by default.
此问题源于gulp@3.9.1
依赖在graceful-fs@^3.0.0
上,它修补了Node.js fs
模块.
This issue stems from the fact that gulp@3.9.1
depends on graceful-fs@^3.0.0
which monkeypatches Node.js fs
module.
此版本以前一直与Node.js一起使用,直到版本11.15(该版本为 version 开发分支,不应在生产中使用.
This used to work with Node.js until version 11.15 (which is a version from a development branch and shouldn't be used in production).
graceful-fs@^4.0.0
不再猴子补丁Node.js fs
模块,使其与Node.js> 11.15兼容.
graceful-fs@^4.0.0
does not monkeypatch Node.js fs
module anymore, which makes it compatible with Node.js > 11.15.
请注意,这不是长期解决方案,但是在您没有时间更新到gulp@^4.0.0
时会有所帮助.
Note that this is not a perennial solution but it helps when you don't have time to update to gulp@^4.0.0
.
这篇关于如何修复ReferenceError:节点中未定义primordials的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!