node.js 如何修复 ReferenceError:原始节点中未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55921442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix ReferenceError: primordials is not defined in node
提问by Ramesh
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.
我已经通过 npm install 安装了节点模块,然后我尝试在命令提示符下执行 gulp sass-watch。之后,我得到了以下回复。
[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
在 gulp sass-watch 之前试过这个
npm -g install gulp-cli
回答by Hassan Hodges
I hit the same error. I suspect you're using node 12 and gulp 3. That combination does not work: https://github.com/gulpjs/gulp/issues/2324
我遇到了同样的错误。我怀疑您正在使用节点 12 和 gulp 3。这种组合不起作用:https: //github.com/gulpjs/gulp/issues/2324
A previous workaround from Jan. does not work either: https://github.com/gulpjs/gulp/issues/2246
Jan 之前的解决方法也不起作用:https: //github.com/gulpjs/gulp/issues/2246
Solution: Either upgrade to gulp 4 or downgrade to an earlier node.
解决方案:升级到 gulp 4 或降级到更早的节点。
回答by Valentin
We encountered the same issue when updating a legacy project depending on [email protected]to Node.js 12.
我们在更新依赖于[email protected]Node.js 12的遗留项目时遇到了同样的问题。
These fixes enable you to use Node.js 12 with [email protected]by overriding graceful-fsto version 4.2.3.
这些修复使您能够[email protected]通过覆盖graceful-fs到 version来使用 Node.js 12 4.2.3。
If your project isn't actively being worked on and you use npm
如果您的项目没有被积极处理并且您使用 npm
Create a npm-shrinkwrap.jsonfilecontaining this:
创建一个包含以下内容的npm-shrinkwrap.json文件:
{
"dependencies": {
"graceful-fs": {
"version": "4.2.3"
}
}
}
Commit this npm-shrinkwrap.jsonfile. And then execute npm installwhich will update the npm-shrinkwrap.jsonfile.
提交此npm-shrinkwrap.json文件。然后执行npm install这将更新npm-shrinkwrap.json文件。
Unfortunately, this solution does not work anymore if you npm installagain. See the other solutions below.
不幸的是,如果您npm install再次使用,此解决方案将不再起作用。请参阅下面的其他解决方案。
If your project is in active development and you use yarn v1
如果您的项目正在积极开发中并且您使用 yarn v1
Yarn v1 supports resolving a package to a defined version.
You need to add a resolutionssection to your package.json:
Yarn v1支持将包解析为定义的版本。您需要将一个resolutions部分添加到您的package.json:
{
// Your current package.json contents
"resolutions": {
"graceful-fs": "4.2.3"
}
}
Thanks @jazdfor this way to solve the issue.
感谢@jazd以这种方式解决问题。
If your project is in active development and you use npm
如果您的项目正在积极开发中并且您使用 npm
Using npm-force-resolutionsas a preinstall script, you can obtain a similar result as with yarn v1. You need to modify your package.json this way:
使用npm-force-resolutions作为一个预安装脚本,就可以得到类似的结果与纱V1。你需要这样修改你的 package.json :
{
// Your current package.json
"scripts": {
// Your current package.json scripts
"preinstall": "npx npm-force-resolutions"
},
"resolutions": {
"graceful-fs": "4.2.3"
}
}
npm-force-resolutionswill alter the package-lock.jsonfile to set graceful-fsto the wanted version before the installis done.
npm-force-resolutions将在完成之前更改package-lock.json文件以设置graceful-fs为所需的版本install。
If you are using a custom .npmrcfile in your project and it contains either a proxy or custom registry, you need to change npx npm-force-resolutionsto npx --userconfig .npmrc npm-force-resolutionsbecause as of now, npxdoesn't use the current folder .npmrcfile by default.
如果您.npmrc在项目中使用自定义文件并且它包含代理或自定义注册表,则需要更改npx npm-force-resolutions为,npx --userconfig .npmrc npm-force-resolutions因为截至目前,默认情况下npx不使用当前文件夹.npmrc文件。
Origin of the problem
问题的根源
This issue stems from the fact that [email protected]dependson graceful-fs@^3.0.0which monkeypatches Node.js fsmodule.
这个问题从事实茎[email protected]依赖于graceful-fs@^3.0.0它monkeypatches Node.js的fs模块。
This used to work with Node.js until version 11.15 (which is a versionfrom a development branch and shouldn't be used in production).
这曾经与 Node.js 一起使用,直到版本 11.15(这是一个来自开发分支的版本,不应在生产中使用)。
graceful-fs@^4.0.0does not monkeypatch Node.js fsmodule anymore, which makes it compatible with Node.js > 11.15.
graceful-fs@^4.0.0不再对 Node.jsfs模块进行猴子补丁,这使其与 Node.js > 11.15 兼容。
Note that this is not a perennial solution but it helps when you don't have time to update to gulp@^4.0.0.
请注意,这不是一个长期的解决方案,但当您没有时间更新到gulp@^4.0.0.
回答by Alphonse R. Dsouza
Use following commands and install node v11.15.0:
使用以下命令并安装node v11.15.0:
npm install -g n
sudo n 11.15.0
will solve
会解决
ReferenceError: primordials is not defined in node
ReferenceError:原始节点未在节点中定义
Referred from @Terje Norderhaug @Tom Corelis answers.
来自@Terje Norderhaug @Tom Corelis 的回答。
回答by Cundong Zhang
Use following commands to install node v11.15.0and gulp v3.9.1:
使用以下命令安装node v11.15.0和gulp v3.9.1:
npm install -g n
sudo n 11.15.0
npm install gulp@^3.9.1
npm install
npm rebuild node-sass
Will solve this issue:
将解决这个问题:
ReferenceError: primordials is not defined in node
回答by Diego Fortes
Fix it in 1 minute:
1分钟解决:
Just follow these steps. I'm on windows 10 and it worked perfectly for me!
只需按照以下步骤操作即可。我在 Windows 10 上,它非常适合我!
- In the same directory where you have
package.jsoncreate anpm-shrinkwrap.jsonfile with the following contents:
- 在您
package.json创建npm-shrinkwrap.json具有以下内容的文件的同一目录中:
{
"dependencies": {
"graceful-fs": {
"version": "4.2.2"
}
}
}
Run
npm install, and don't worry, it'll updatenpm-shrinkwrap.jsonwith a bunch of content.Run
gulpto start the project.
运行
npm install,别担心,它会更新npm-shrinkwrap.json一堆内容。运行
gulp以启动项目。
回答by kevnk
回答by Aymen Yaseen
Gulp 3.9.1 doesn't work with Node v12.x.x, and if you upgrade to Gulp 4.0.2, you have to completely change gulpfile.js with the new Syntax (Series & Parallels). So your best bet is to downgrade to Node V 11.x.x, the 11.15.0 worked fine for me. By simply using the following code in terminal:
Gulp 3.9.1 不适用于 Node v12.xx,如果您升级到 Gulp 4.0.2,则必须使用新语法(Series & Parallels)彻底更改 gulpfile.js。所以你最好的办法是降级到 Node V 11.xx,11.15.0 对我来说很好用。只需在终端中使用以下代码:
nvm install 11.15.0
nvm use 11.15.0 #just in case it didn't automatically select the 11.15.0 as the main node.
nvm uninstall 13.1.0
npm rebuild node-sass
Cheers!
干杯!
回答by Ravi Anand
had same error and finally fix that when updated all packages and then mentioned the same node engine version and npm versionin package.jsonas it is in my local working system.
有同样的错误,终于修复程序更新的包时,然后提到的同一节点引擎版本和故宫的版本中package.json,因为它是在我的本地工作系统。
"engines": {
"node": "10.15.3",
"npm": "6.9.0"
}
i was getting this error when deploying on heroku.
在heroku上部署时出现此错误。
for more checkout heroku support
更多结帐heroku支持
回答by Tom Corelis
Downgrading to node stable fixed this issue for me, as it occurred after I upgraded to node 12
降级到稳定节点为我解决了这个问题,因为它发生在我升级到节点 12 之后
sudo n 10.16.0
sudo n 10.16.0
回答by Oliver
TL:DR
翻译:博士
Gulp 3.*doesn't work on Node 12.*or above. You have to downgrade Node, or upgrade Gulp.
Gulp3.*不适用于 Node12.*或更高版本。你必须降级 Node,或者升级 Gulp。
If you are short on time, downgrade Node to v11.* or below; if you need newer features, and have time to possibly fix a load of broken dependencies, upgrade Gulp to 4.* or above!
如果时间不够,可以将 Node 降级到 v11.* 或以下;如果您需要更新的功能,并且有时间修复大量损坏的依赖项,请将 Gulp 升级到 4.* 或更高版本!
As others have already mentioned, Gulp 3.*is not supported on Node 12or above, so you will have to downgrade your Node version to 11.*or below, OR upgrade your Gulp to 4.0.
正如其他人已经提到的,3.*Node12或更高版本不支持Gulp ,因此您必须将 Node 版本降级到11.*或更低版本,或者将 Gulp 升级到4.0.
The best option depends ultimately on how much time you have, as upgrading Gulp brings benefits of cleaner gulpfiles and in-built control over having tasks run in series or parallel, but also relies on you re-writing your gulpfile to a new syntax, and might(read: probably will - see end of this comment) cause conflicts with some dependencies.
最好的选择最终取决于你有多少时间,因为升级 Gulp 带来了更干净的 gulpfiles 和对让任务串行或并行运行的内置控制的好处,但也依赖于你将 gulpfile 重写为新的语法,并且可能(阅读:可能会 - 请参阅此评论的结尾)会导致与某些依赖项发生冲突。
Downgrading Node
降级节点
This is the easiest and quickest option. Especially if you use nor nvm, as these allow you to very quick install and switch between Node versions.
这是最简单、最快捷的选择。特别是如果您使用n或nvm,因为它们允许您非常快速地安装和在 Node 版本之间切换。
Installing Node version on N
在 N 上安装 Node 版本
n 10.16.0
InstallingNode version on NVM
在 NVM 上安装 Node 版本
nvm install 10.16.0
One you have done this, you mayneed to rebuild your npm dependenciesor alternatively remove both your node_modulesfolder AND your package-lock.jsonfile and re-installing your dependencies. Though if you are merely reverting to a pre-existing Node version, you should probably be fine.
如果您已经这样做了,您可能需要重建您的npm 依赖项,或者删除您的node_modules文件夹和package-lock.json文件并重新安装您的依赖项。但是,如果您只是恢复到预先存在的 Node 版本,那么您应该没问题。
Upgrading Gulp
升级 Gulp
As mentioned above, this is a more time-intensive task, but might bring benefits in the long-run. For example, Node 12has now introduced native support for ES Modules (behind an experimental flag) and full support in Node 13.
如上所述,这是一项更耗时的任务,但从长远来看可能会带来好处。例如,Node12现在引入了对 ES 模块的原生支持(在一个实验性标志后面)和 Node 的完全支持13。
You may need to upgrade Node to use that, forcing you to upgrade Gulp. Or you may simply want the benefits of using Gulp 4, as it offers better and more efficient control over writing tasks.
您可能需要升级 Node 才能使用它,从而迫使您升级 Gulp。或者您可能只是想要使用 Gulp 4 的好处,因为它提供了对编写任务的更好和更有效的控制。
There are a number of articles on this already, so I won't elaborate any further on the specifics, but to reiterate - this is not a quick job. Depending on the size of your project, there may be some notable re-writing required, and you may have dependencies that break. If you are in short supply of time, you should opt for simply downgrading Node, at least temporarily.
已经有很多关于这方面的文章,所以我不会再详细说明具体细节,但要重申 -这不是一项快速的工作。根据您的项目的大小,可能需要一些值得注意的重写,并且您可能有依赖关系中断。如果您的时间不够用,您应该选择简单地降级 Node,至少是暂时降级。
But I already have Gulp 4, and it still doesn't work!
但是我已经有了 Gulp 4,它仍然不起作用!
If, like me, you are already using Gulp 4+ (I was using Gulp 4.0.2, originally on Node 10) and have recently upgraded (I upgraded to Node 13.8.0) are you are still getting the issue, it may be because a dependency is relying on an older version of Gulp, and that is getting caught in the pipeline.
如果像我一样,您已经在使用 Gulp 4+(我使用的是 Gulp 4.0.2,最初在 Node 10 上)并且最近升级(我升级到Node 13.8.0)您是否仍然遇到问题,这可能是因为依赖项依赖于旧版本的 Gulp,它正在被困在管道中。
In my case, gulp-combine-mqwas a dependency using Gulp 3.9.*. Disabling this task in my gulpfile allowed Gulp to run again.
就我而言,gulp-combine-mq是使用 Gulp 的依赖项3.9.*。在我的 gulpfile 中禁用此任务允许 Gulp 再次运行。
If this happens, you have a few options: you can,
如果发生这种情况,您有几个选择:您可以,
- Go without the plugin if it's not absolutely necessary
- Find an alternative,
- Fix the plugin
- 如果不是绝对必要,请不要使用插件
- 寻找替代品,
- 修复插件
Needless to say, if you have several plugins that rely on older version of Gulp - especially if these plugins are vital for your application - this is where there can be a huge additional chunk of time spent in upgrading Gulp (hence the warnings above).
不用说,如果您有几个依赖于旧版 Gulp 的插件——尤其是如果这些插件对您的应用程序至关重要——那么升级 Gulp 可能会花费大量额外的时间(因此出现上述警告)。
If this happens, it is best to just downgrade Node, at least until patches can be issued.
如果发生这种情况,最好只降级 Node,至少在补丁发布之前。

