简体   繁体   English

如何修复 Node.js 中的“ReferenceError:primordials is not defined”

[英]How to fix "ReferenceError: primordials is not defined" in Node.js

I have installed Node.js modules by 'npm install', and then I tried to do gulp sass-watch in a command prompt.我已经通过“npm install”安装了 Node.js 模块,然后我尝试在命令提示符下执行gulp sass-watch 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

I have tried this before gulp sass-watch :我在gulp sass-watch之前尝试过这个:

npm -g install gulp-cli

I hit the same error.我遇到了同样的错误。 I suspect you're using Node.js 12 and Gulp.js 3. That combination does not work: Gulp.js 3 is broken on Node.js 12 #2324 I suspect you're using Node.js 12 and Gulp.js 3. That combination does not work: Gulp.js 3 is broken on Node.js 12 #2324

A previous workaround from Jan. does not work either: After update to Node.js 11.0.0 running Gulp.js exits with 'ReferenceError: internalBinding is not defined' #2246 1 月以前的解决方法也不起作用:更新到 Node.js 11.0.0 后,运行 Gulp.js 退出并出现“ReferenceError:internalBinding is not defined”#2246

Solution: Either upgrade to Gulp.js 4 or downgrade to an earlier version of Node.js.解决方案:升级到 Gulp.js 4 或降级到 Node.js 的早期版本。

We encountered the same issue when updating a legacy project depending on gulp@3.9.1 to Node.js 12+.在将依赖gulp@3.9.1的旧项目更新到 Node.js 12+ 时,我们遇到了同样的问题。

These fixes enable you to use Node.js 12+ with gulp@3.9.1 by overriding graceful-fs to version ^4.2.9 .这些修复使您能够通过将graceful-fs覆盖到版本^4.2.9来使用 Node.js 12+ 和gulp@3.9.1

If you are using yarn v1如果您使用的是纱线 v1

Yarn v1 supports resolving a package to a defined version . Yarn v1 支持将 package 解析为定义的版本 You need to add a resolutions section to your package.json :您需要在package.json中添加resolutions部分:

{
  // Your current package.json contents
  "resolutions": {
    "graceful-fs": "^4.2.9"
  }
}

Thanks @jazd for this way to solve the issue.感谢@jazd以这种方式解决问题。

If you are using npm >= 8.3.0如果您使用的是 npm >= 8.3.0

npm@^8.3.0 enables you to override the version of a package of your project's dependencies. npm@^8.3.0使您能够覆盖项目依赖项的 package 版本。 To do so, you should add an overrides section in your package.json:为此,您应该在 package.json 中添加overrides部分

{
  // Your current package.json
  "overrides": {
    "graceful-fs": "^4.2.9"
  }
}

If you are using npm < 8.3.0如果您使用的是 npm < 8.3.0

Using npm-force-resolutions as a preinstall script, you can obtain a similar result as with yarn v1.使用npm-force-resolutions作为预安装脚本,您可以获得与使用 yarn v1 类似的结果。 You need to modify your package.json this way:您需要以这种方式修改 package.json:

{
  // Your current package.json
  "scripts": {
    // Your current package.json scripts
    "preinstall": "npx npm-force-resolutions"
  },
  "resolutions": {
    "graceful-fs": "^4.2.9"
  }
}

npm-force-resolutions will alter the package-lock.json file to set graceful-fs to the wanted version before the install is done. npm-force-resolutions将更改package-lock.json文件以在install完成之前将graceful-fs设置为所需版本。

If you are using a custom .npmrc file in your project and it contains either a proxy or custom registry, you might 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.如果您在项目中使用自定义.npmrc文件并且它包含代理或自定义注册表,则您可能需要将 npx npm-force- npx npm-force-resolutions更改为npx --userconfig.npmrc npm-force-resolutions因为到目前为止,默认情况下, .npmrc npx

Origin of the problem问题的根源

This issue stems from the fact that gulp@3.9.1 depends on graceful-fs@^3.0.0 which monkeypatches Node.js fs module.这个问题源于gulp@3.9.1依赖graceful-fs@^3.0.0的猴子补丁 Node.js fs模块的事实。

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).这曾经与 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 (tested and working with versions 12 and 14). graceful-fs@^4.0.0不再对 Node.js fs模块进行猴子补丁,这使其与 Node.js > 11.15 兼容(测试并使用版本 12 和 14)。

Note that this is not a perennial solution but it helps when you don't have the time to update to gulp@^4.0.0 .请注意,这不是一个长期的解决方案,但当您没有时间更新到gulp@^4.0.0时,它会有所帮助。

Fix it in one minute:一分钟搞定:

Just follow these steps .只需按照这些步骤 I'm on Windows 10 and it worked perfectly for me我在 Windows 10 上,它非常适合我

  1. In the same directory where you have package.json create a npm-shrinkwrap.json file with the following contents:在您拥有package.json的同一目录中,创建一个npm-shrinkwrap.json shrinkwrap.json 文件,其内容如下:

     { "dependencies": { "graceful-fs": { "version": "4.2.2" } } }
  2. Run npm install , and don't worry, it will update npm-shrinkwrap.json with a bunch of content.运行npm install ,别担心,它会用一堆内容更新npm-shrinkwrap.json shrinkwrap.json 。

  3. Run gulp to start the project.运行gulp启动项目。

Use the following commands and install Node.js v11.15.0 :使用以下命令并安装Node.js v11.15.0

npm install -g n

sudo n 11.15.0

will solve将解决

ReferenceError: primordials is not defined in node ReferenceError: primordials 未在节点中定义

Referred from @Terje Norderhaug @Tom Corelis answers.参考@Terje Norderhaug @Tom Corelis 的答案。

Use the following commands to install Node.js v11.15.0 and Gulp.js v3.9.1:使用以下命令安装 Node.js v11.15.0 和 Gulp.js v3.9.1:

npm install -g n

sudo n 11.15.0

npm install gulp@^3.9.1
npm install
npm rebuild node-sass

It will solve this issue:它将解决这个问题:

ReferenceError: primordials is not defined in node ReferenceError: primordials 未在节点中定义

For me, Diego Fortes' answer works with one small change.对我来说, Diego Fortes 的回答只需稍加改动即可。

Here is my workflow if this error appears:如果出现此错误,这是我的工作流程:

  1. npm install

  2. npm install gulp

  3. create file npm-shrinkwrap.json with创建文件npm-shrinkwrap.json

    { "dependencies": { "graceful-fs": { "version": "4.2.2" } } }
  4. npm install (again) (Not npm install gulp again Very important - otherwise the error will be come back) npm install (再次)(不是npm install gulp非常重要 - 否则错误会回来)

  5. gulp (now working) gulp(正在工作)

Using NVM to manage what Node.js version you're using, running the following commands worked for me:使用NVM管理您正在使用的 Node.js 版本,运行以下命令对我有用:

cd /to/your/project/
nvm install lts/dubnium
nvm use lts/dubnium
yarn upgrade # or `npm install`

TL:DR TL:博士

Gulp 3.* doesn't work on Node.js 12.* or above. Gulp 3.*不适用于 Node.js 12.*或更高版本。 You have to downgrade Node.js, or upgrade Gulp.您必须降级 Node.js,或升级 Gulp。

If you are short on time, downgrade Node.js to v11.* or below;如果时间紧迫,请将 Node.js 降级到 v11.* 或以下; if you need newer features, and have time to possibly fix a load of broken dependencies, upgrade Gulp to 4.* or above如果您需要更新的功能,并且有时间修复大量损坏的依赖项,请将 Gulp 升级到 4.* 或更高版本

As others have already mentioned, Gulp 3.* is not supported on Node.js 12 or above, so you will have to downgrade your Node version to 11.* or below, OR upgrade your Gulp to 4.0.正如其他人已经提到的,Node.js 12 或更高版本不支持 Gulp 3.*,因此您必须将 Node 版本降级到 11.* 或更低版本,或者将您的 ZF5700A296CF2DA0C74D90F780.AE8 升级到

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 rewriting your gulpfile to a new syntax, and might (read: probably will - see end of this comment) cause conflicts with some dependencies.最好的选择最终取决于你有多少时间,因为升级 Gulp 带来了更干净的 gulpfile 和对任务串行或并行运行的内置控制的好处,但也依赖于你将 gulpfile 重写为新语法,并且可能(阅读:可能会 - 看到此评论的结尾)导致与某些依赖项发生冲突。


Downgrading Node.js降级 Node.js

This is the easiest and quickest option.这是最简单和最快的选择。 Especially if you use n or nvm , as these allow you to very quick install and switch between Node.js versions.特别是如果您使用nnvm ,因为它们允许您非常快速地安装和在 Node.js 版本之间切换。

Installing Node.js version on N N上安装Node.js版本

n 10.16.0

Installing a Node.js version on NVM在 NVM 上安装 Node.js 版本

nvm install 10.16.0

Once you have done this, you may need to rebuild your npm dependencies or alternatively remove both your node_modules folder and your package-lock.json file and reinstalling your dependencies.完成此操作后,您可能需要重建npm 依赖项,或者删除node_modules文件夹package-lock.json文件并重新安装依赖项。 Though if you are merely reverting to a preexisting Node.js version, you should probably be fine.虽然如果您只是恢复到先前存在的 Node.js 版本,您应该没问题。


Upgrading Gulp升级Gulp

As mentioned above, this is a more time-intensive task, but it might bring benefits in the long run.如上所述,这是一项更耗时的任务,但从长远来看,它可能会带来好处。 For example, Node.js 12 has now introduced native support for ES Modules (behind an experimental flag) and full support in Node.js 13.例如,Node.js 12 现在引入了对 ES 模块的本机支持(在实验标志后面)和 Node.js 13 中的完全支持。

You may need to upgrade Node.js to use that, forcing you to upgrade Gulp.您可能需要升级 Node.js 才能使用它,迫使您升级 Gulp。 Or you may simply want the benefits of using Gulp 4, as it offers better and more efficient control over writing tasks.或者您可能只是想要使用 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 rewriting required, and you may have dependencies that break.根据您项目的大小,可能需要进行一些显着的重写,并且您的依赖项可能会中断。 If you are in short supply of time, you should opt for simply downgrading Node.js, at least temporarily.如果您时间不够用,您应该选择简单地降级 Node.js,至少是暂时的。


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.js 10) and have recently upgraded (I upgraded to Node.js 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. If, like me, you are already using Gulp 4+ (I was using Gulp 4.0.2 , originally on Node.js 10) and have recently upgraded (I upgraded to Node.js 13.8.0) are you are still getting the issue, it may be因为依赖项依赖于旧版本的 Gulp,而这正陷入困境。

In my case, gulp-combine-mq was a dependency using Gulp 3.9.*.就我而言, gulp-combine-mq是使用 Gulp 3.9.* 的依赖项。 Disabling this task in my gulpfile allowed Gulp to run again.在我的 gulpfile 中禁用此任务允许 Gulp 再次运行。

If this happens, you have a few options.如果发生这种情况,您有几个选择。 You can,你可以,

  1. Go without the plugin if it's not absolutely necessary Go 没有插件,如果不是绝对必要的话
  2. Find an alternative,寻找替代方案,
  3. Fix the plugin修复插件

Needless to say, if you have several plugins that rely on an 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.js, at least until patches can be issued.如果发生这种情况,最好只降级 Node.js,至少在可以发布补丁之前。

Simple and elegant solution简单优雅的解决方案

Just follow these steps.只需按照以下步骤操作。 It worked perfectly with npm install running multiple times or installing any other modules or even publishing project to artifactory.它与 npm 安装运行多次或安装任何其他模块甚至发布项目到工件完美配合。

In the same directory where you have package.json create a npm-shrinkwrap.json file 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 update npm-shrinkwrap.json with a bunch of content.运行 npm 安装,别担心,它会用一堆内容更新 npm-shrinkwrap.json。 Let's get rid of this updates by updating package.json scripts options.让我们通过更新package.json脚本选项来摆脱这个更新。

"scripts": {
    "preshrinkwrap": "git checkout -- npm-shrinkwrap.json",
    "postshrinkwrap": "git checkout -- npm-shrinkwrap.json"
}

Now you can run npm install and your npm-shrinkwrap.json will be intact and will work forever.现在您可以运行 npm 安装,并且您的 npm-shrinkwrap.json 将完好无损并且将永远工作。

Gulp 3.9.1 doesn't work with Node v12.xx, and if you upgrade to Gulp 4.0.2, you have to completely change gulpfile.js with the new syntax (series & parallels). Gulp 3.9.1 不适用于 Node v12.xx,如果升级到 Gulp 4.0.2,则必须使用新语法(系列和并行)完全更改 gulpfile.js。 So your best bet is to downgrade to Node.js v 11.xx (the 11.15.0 version worked fine for me) by simply using the following code in a terminal:所以你最好的选择是通过在终端中简单地使用以下代码来降级到 Node.js 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

I had the same error.我有同样的错误。 I finally fixed that when I updated all packages and then mentioned the same Node.js engine version and npm version in package.json as it is in my local working system. I finally fixed that when I updated all packages and then mentioned the same Node.js engine version and npm version in package.json as it is in my local working system.

 "engines": {
    "node": "10.15.3",
    "npm": "6.9.0"
 }

I was getting this error when deploying on Heroku .Heroku上部署时出现此错误。

For more, check out Heroku support .有关更多信息,请查看Heroku 支持

Check Node.js version:查看 Node.js 版本:

 node --version

Check gulp version:检查 gulp 版本:

gulp -v

If Node.js >=12 and gulp <= 3, do one of the following:如果 Node.js >=12 和 gulp <= 3,请执行以下操作之一:

  1. Upgrade gulp升级gulp
sudo npm install -g gulp
  1. Downgrade node降级节点
sudo npm install -g n
sudo n 11.15.0

How to Upgrade (or Downgrade) Node.js Using npm 如何使用 npm 升级(或降级)Node.js

In case the problem is not from gulp then check the unzip npm module.如果问题不是来自gulp则检查解压缩npm 模块。 It's been around six years since the last time it was updated.自上次更新以来已经过去了大约六年。 It doesn't work with Node.js > v11.它不适用于 Node.js > v11。

Try this:尝试这个:

npm install -g n

sudo n 11.15.0

This error is because of the new version of Node.js (12) and an old version of Gulp (less than 4).这个错误是因为新版本的Node.js (12)和旧版本的Gulp (小于4)。

Downgrading Node.js and other dependencies isn't recommended.不建议降级 Node.js 和其他依赖项。 I solved this by updating package.json file, fetching the latest version of all dependencies.我通过更新package.json文件解决了这个问题,获取所有依赖项的最新版本。 For this, I use npm-check-updates .为此,我使用npm-check-updates It is a module that updates the package.json with the latest version of all dependencies.它是一个使用所有依赖项的最新版本更新package.json的模块。

Reference : https://www.npmjs.com/package/npm-check-updates参考https://www.npmjs.com/package/npm-check-updates

npm i -g npm-check-updates
ncu -u
npm install

In most cases, we will have to update the gulpfile.js as well like the following:在大多数情况下,我们必须更新gulpfile.js ,如下所示:

Reference : Gulp 4: The new task execution system - gulp.parallel and gulp.series, Migration参考Gulp 4:新的任务执行系统——gulp.parallel和gulp.series,迁移

Before:前:

gulp.task(
    'sass', function () {
        return gulp.src([sourcePath + '/sass/**/*.scss', "!" + sourcePath + "/sass/**/_*.scss"])

            ....
    }
);

Other configuration...

gulp.task(
    'watch', function () {
        gulp.watch(sourcePath + '/sass/**/*.scss', ['sass']);
    }
);

After:后:

gulp.task('sass', gulp.series(function(done) {
    return gulp.src([sourcePath + '/sass/**/*.scss', "!" + sourcePath + "/sass/**/_*.scss"])

            ...

    done();
}));

Other config...

gulp.task(
    'watch', function () {
        gulp.watch(sourcePath + '/sass/**/*.scss', gulp.series('sass'));
    }
);

The problem occurred for me in Visual Studio's Task Runner Explorer only and not when running from the command line or PowerShell.我的问题仅出现在 Visual Studio 的Task Runner Explorer中,而不是从命令行或 PowerShell 运行时。

I realised that VS was ignoring the Node version I had set with NVM .我意识到 VS 忽略了我用NVM设置的 Node 版本。

This post gave the answer: Configure which NPM is used by Visual Studio's Task Runner Explorer?这篇文章给出了答案: Configure which NPM is used by Visual Studio's Task Runner Explorer? by setting the PATH variable as a higher priority than external tools in VS, it used the Node version set by NVM and not the version installed with VS.通过将PATH变量设置为比 VS 中的外部工具更高的优先级,它使用了 NVM 设置的 Node 版本,而不是 VS 安装的版本。 在此处输入图像描述

I faced the same issue.我遇到了同样的问题。 What I tried and what worked for me:我尝试了什么,什么对我有用:

  1. Check the version of Node.js and Gulp.js (a combination of Node.js v12 and Gulp.js less than v4 doesn't work)检查Node.jsGulp.js的版本(Node.js v12 和 ZF5700A2964CF2DA0B4D90F7 的组合不工作)

  2. I downgraded the NPM version by:我通过以下方式降级了 NPM 版本:

     sudo NPM install -gn sudo n 10.16.0

It worked fine.它工作得很好。 Then just follow the instructions of your console.然后只需按照控制台的说明进行操作即可。

Downgrading to Node.js stable fixed this issue for me, as it occurred after I upgraded to Node.js 12:降级到 Node.js stable 为我解决了这个问题,因为它发生在我升级到 Node.js 12 之后:

sudo n 10.16.0

I was getting this error on Windows 10. It turned out to be a corrupted roaming profile.我在 Windows 10 上收到此错误。原来是漫游配置文件损坏。

npm ERR! node v12.4.0
npm ERR! npm  v3.3.12

npm ERR! primordials is not defined
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:

Deleting the C:\Users\{user}\AppData\Roaming\npm folder fixed my issue.删除C:\Users\{user}\AppData\Roaming\npm文件夹解决了我的问题。

If keeping Node.js v12 while using the latest gulp ^4.0, follow these steps:如果在使用最新的 gulp ^4.0 的同时保留 Node.js v12,请执行以下步骤:

Update the command-line interface (just for precaution) using:使用以下命令更新命令行界面(只是为了预防起见):

npm i gulp-cli -g

Add/Update the gulp under dependencies section of your package.json filepackage.json文件的依赖项部分下添加/更新gulp

"dependencies": {
  "gulp": "^4.0.0"
}

Delete your package-lock.json file.删除您的package-lock.json文件。

Delete your node_modules folder.删除你的node_modules文件夹。

Finally, run npm i to upgrade and recreate a brand new node_modules folder and package-lock.json file with correct parameters for Gulp ^4.0:最后,运行npm i升级并重新创建一个全新的node_modules文件夹和package-lock.json文件,并为 Gulp ^4.0 提供正确的参数:

npm i

Note Gulp.js 4.0 introduces the series() and parallel() methods to combine tasks instead of the array methods used in Gulp 3, and so you may or may not encounter an error in your old gulpfile.js script.注意Gulp.js 4.0 引入了series()parallel()方法来组合任务,而不是 Gulp 3 中使用的数组方法,因此您可能会或可能不会在旧的gulpfile.js脚本中遇到错误。

To learn more about applying these new features, this site have really done justice to it: How to Migrate to Gulp.js 4.0要了解有关应用这些新功能的更多信息,此站点确实做到了:如何迁移到 Gulp.js 4.0

I fixed this issue on Windows 10 by uninstalling Node.js from Add or Remove Programs → Node.js.我通过从添加或删除程序→ Node.js 卸载 Node.js 在 Windows 10 上修复了此问题。

Then I installed version 11.15.0 from https://nodejs.org/download/release/v11.15.0/然后我从https://nodejs.org/download/release/v11.15.0/安装了 11.15.0 版本

Choose node-v11.15.0-x64.msi if you're running Windows 64-bit.如果您正在运行 Windows 64 位,请选择node-v11.15.0-x64.msi

If you're trying to install semantic-ui and the following error occurs then try downloading the latest version of Node.js js(13.5.0) with the latest features, from Node.js.org.如果您尝试安装semantic-ui并且出现以下错误,请尝试从 Node.js.org 下载具有最新功能的 Node.js js(13.5.0)的最新版本。

Moreover, rather than trying the NPM install semantic, you should just add the link (which you can find from the cdnjs link ) to the header of your index.html file.此外,与其尝试 NPM 安装语义,不如将链接(可以从cdnjs 链接中找到)添加到index.html文件的 header 中。

Gulp is making issue with Node.js version 11 and above. Gulp 与 Node.js 版本 11 及更高版本有关。 Uninstall your current Node.js version and reinstall the v10.15.1 version.卸载您当前的 Node.js 版本并重新安装v10.15.1版本。 Here is the link for that version.这是该版本的链接。 This helps me and it will solve your problem too.这对我有帮助,也可以解决您的问题。

https://nodejs.org/download/release/v10.15.1/ https://nodejs.org/download/release/v10.15.1/

I have tried a lot of suggestions to fix this problem for an existing project on my Windows 10 machine and ended up following these steps to fix it;我已经尝试了很多建议来解决我的 Windows 10 机器上的现有项目的这个问题,并最终按照这些步骤来解决它;

  • Uninstall Node.js from "Add or remove programs".从“添加或删除程序”中卸载 Node.js。 Fire up a new Command prompt and type gulp -v and then node -v to check that it has been uninstalled completely.启动一个新的命令提示符并输入gulp -v然后node -v检查它是否已完全卸载。
  • Download and install Node.js v10.16.0 - not the latest as latest node & gulp combination is causing the problem as far as I see.下载并安装 Node.js v10.16.0 - 不是最新的,因为我看到的最新节点和 gulp 组合导致了问题。 During installation I didn't change the installation path which I normally do(C:\Program Files\nodejs).在安装过程中,我没有更改我通常做的安装路径(C:\Program Files\nodejs)。
  • Open up a new Command prompt, go to your project's directory where you have got your gulpfile.js and start gulp as shown in the image.如图所示,打开一个新的命令提示符 go 到您的项目目录,其中您有 gulpfile.js 并启动 gulp。

Please note sometimes when I switch between git branches I might need to close my Visual Studio and run it as Admin again in order to see this solution working again.请注意,有时当我在 git 分支之间切换时,我可能需要关闭我的 Visual Studio 并再次以管理员身份运行它才能看到此解决方案再次运行。

As far as I see this problem started to happen after I installed the latest recommended version(12.18.4) of Node.js for a new project and I only realised this when some FE changes weren't reflected on the existing web project.据我所知,在我为一个新项目安装了 Node.js 的最新推荐版本(12.18.4)后,这个问题开始发生,我只有在现有的 web 项目上没有反映一些 FE 更改时才意识到这一点。

Update: Today I had the same problem while setting up one of my existing projects on my new PC and I did the same steps + went to the directory where I have the gulpfile and then run npm install .更新:今天我在新 PC 上设置现有项目之一时遇到了同样的问题,我做了同样的步骤 + 转到我有 gulpfile 的目录,然后运行npm install

在此处输入图像描述

Install gulp and add your Node.js version to the package.json file like so:安装 gulp 并将您的 Node.js 版本添加到package.Z466DEEC76ECDF5FCA65​​D38571F6324文件中

{
  "dependencies": {
    "node":  "^10.16.3"
  }
}

It seems you've upgraded your nodejs's version to be +12 and still using gulp 3.9.1看来您已将 nodejs 的版本升级为 +12 并且仍在使用 gulp 3.9.1

You can use one of the below solutions您可以使用以下解决方案之一

  • Upgrade you glup version to be +4将您的 gup 版本升级为 +4
  • Or simply you use NVM Node version Manager To run multiple nodejs versions on the same machine.或者干脆你使用NVM Node version Manager 在同一台机器上运行多个 nodejs 版本。

I had this very same error, but it was caused by a different issue.我遇到了同样的错误,但它是由不同的问题引起的。

OS: windows 10
nodejs version: 15.12.0
npm version: 7.6.3

The cause of the problem was graceful-fs package.问题的原因是优雅-fs package。 Whenever I tried to run npm, even running npm-v was firing "ReferenceError: primordials is not defined".每当我尝试运行 npm 时,即使运行 npm-v 也会触发“ReferenceError: primordials is not defined”。

I tried running npm install graceful-fs@latest, but it still didn't work, even though the package was latest version.我尝试运行 npm install graceful-fs@latest,但它仍然不起作用,即使 package 是最新版本。

So what helped me?那么是什么帮助了我呢?

run npm ls graceful-fs运行npm ls graceful-fs

This way you'll find all packages on which graceful-fs is dependency and which version it has.通过这种方式,您将找到所有graceful-fs包以及它具有的版本。 In my case it was mostly version 3.0, even though I've installed version 4.2.6就我而言,它主要是 3.0 版,即使我已经安装了 4.2.6 版

So how to fix it?那么如何解决呢?

Open npm-shrinkwrap.json (not sure about packages-lock.json) and change search for graceful-fs - you'll see that it has older versions on a few places.打开 npm-shrinkwrap.json (不确定packages-lock.json)并更改搜索graceful-fs - 你会看到它在一些地方有旧版本。 Replace it with ^4.2.6 (or newer).将其替换为^4.2.6 (或更新版本)。

Then npm audit fix --force which will forcefully install the newer version everywhere.然后npm audit fix --force将在任何地方强制安装较新版本。

Hope this works for you, it took me a few hours to find out how to fix it.希望这对您有用,我花了几个小时才找到解决方法。

  • Remove package-lock.json or yarn.lock file.删除 package-lock.json 或 yarn.lock 文件。

  • Then remove node_modules.然后删除 node_modules。

  • After that modify the package.json file-之后修改 package.json 文件-

    "dependencies": { "gulp": "^4.0.0" } “依赖项”:{“gulp”:“^4.0.0”}

  • Then run- npm install然后运行- npm install

It will be enough to solve this problem.解决这个问题就足够了。

For those who are using Yarn :对于那些使用Yarn的人:

yarn global add n
n 11.15.0
yarn install # Have to install again

You have two options here你在这里有两个选择

  1. Either upgrade to gulp 4 or else要么升级到 gulp 4 要么
  2. downgrade to an earlier Node.js version.降级到更早的 Node.js 版本。

This is because the compatibility issue between node and gulp in your system.这是因为你的系统中nodegulp之间的兼容性问题。 Downgrading the node or upgrading the gulp will fix this issue.降级node或升级gulp将解决此问题。

sudo npm i -g n
sudo n 11.15.0

Try removing the node_modules folder and package-lock.json file and installing again using npm i command if still not working.如果仍然无法正常工作,请尝试删除node_modules文件夹和package-lock.json文件并使用npm i命令再次安装。

I got the same issue installing the npm package webshot .我在安装 npm package webshot 时遇到了同样的问题。

NOTE: it was a known issue for that package as it depends on graceful-fs behind the scenes.注意:这是 package 的一个已知问题,因为它依赖于幕后的优雅 fs。

Fix: 1. upgrade graceful-fs to 4.x or higher修复: 1.将 graceful-fs 升级到 4.x 或更高版本

Fix: 2. use webshot-node instead https://www.npmjs.com/package/webshot-node修复:2.使用 webshot-node 代替https://www.npmjs.com/package/webshot-node

Since my project was using gulp version 4, I had to do the following to solve this由于我的项目使用的是 gulp 版本 4,因此我必须执行以下操作来解决此问题

  1. Delete folder node_modules删除文件夹node_modules
  2. open package.json and update version打开 package.json 并更新版本

问题

更新

Here is the detail of version I am using这是我正在使用的版本的详细信息

版本

Now run npm install and then run gulp default.现在运行 npm install 然后运行 ​​gulp 默认。 The error should be gone and you may see:错误应该消失了,您可能会看到:

Task never defined: default only.任务从未定义:仅默认值。

For me after using series() to combine task the problem was solved.对我来说,在使用 series() 组合任务后,问题就解决了。

Any of the answer here was not solved the problem.这里的任何答案都没有解决问题。

uninstall node and reinstall it using given link. 卸载节点,然后使用给定的链接重新安装它。 https://nodejs.org/en/download/ https://nodejs.org/en/download/

I suggest you first make sure NPM install is not your problem.我建议您首先确保NPM 安装不是您的问题。 Then you downgrade Node.js and Gulp.js versions.然后降级 Node.js 和 Gulp.js 版本。 I used Node.js 10.16.1 and Gulp.js 3.9.1.我使用了 Node.js 10.16.1 和 Gulp.js 3.9.1。

To downgrade your Gulp.js installation, you can try:要降级 Gulp.js 安装,您可以尝试:

npm install gulp@^3.9.1

Using Python 2 (executable python ) during npm installation worked for me:npm安装期间使用 Python 2(可执行python )对我有用:

npm install --python=~/venv/bin/python

We also get this error when we use the s3 NPM package.当我们使用 s3 NPM package 时,我们也会收到此错误。 So the problem is with the graceful-fs package - we need to update it.所以问题在于优雅的 fs package - 我们需要更新它。 It is working fine on 4.2.3.它在 4.2.3 上运行良好。

So just look in what NPM package it is showing in the logs trace and update graceful-fs accordingly to 4.2.3.因此,只需查看它在日志跟踪中显示的 NPM package 并相应地将优雅 fs 更新为 4.2.3。

For anyone having the same error for the same reason in ADOS CI Build:对于在 ADOS CI Build 中出于相同原因出现相同错误的任何人:

This question was the first I found when looking for help.这个问题是我在寻求帮助时发现的第一个问题。 I have an ADOS CI build pipeline where the first Node.js tool installer task is used to install Node.js.我有一个 ADOS CI 构建管道,其中第一个 Node.js 工具安装程序任务用于安装 Node.js。 Then npm task is used to install Gulp.js (npm install -g gulp).然后 npm 任务用于安装 Gulp.js (npm install -g gulp)。 Then the following Gulp.js task runs default-task from gulpfile.js.然后下面的 Gulp.js 任务从 gulpfile.js 运行 default-task。 There's some gulp-sass stuff in it.里面有一些咕噜咕噜的东西。

When I changed the Node.js tool to install 12.x latest node instead of an older one and the latest Gulp.js version was 4.0.2.当我更改 Node.js 工具以安装 12.x 最新节点而不是较旧的节点时,最新的 Gulp.js 版本是 4.0.2。 The result was the same error as described in the question.结果与问题中描述的错误相同。

What worked for me in this case was to downgrade Node.js to latest 11.x version as was already suggested by Alphonse R.在这种情况下,对我有用的是将 Node.js 降级到最新的 11.x 版本,正如 Alphonse R 已经建议的那样。 Dsouza and Aymen Yaseen. Dsouza 和 Aymen Yaseen。 In this case though there's no need to use any commands they suggested, but rather just set the Node.js tool installer version spec to latest Node.js version from 11.x.在这种情况下,尽管不需要使用他们建议的任何命令,而只需将 Node.js 工具安装程序版本规范设置为 11.x 中的最新 Node.js 版本。

在此处输入图像描述

The exact version of Node.js that got installed and is working was 11.15.0.已安装并正在运行的 Node.js 的确切版本是 11.15.0。 I didn't have to downgrade Gulp.js.我不必降级 Gulp.js。

I was also getting an error on Node.js 12/13 with Gulp.js 3. Moving to Node.js 11 worked.我在 Node.js 12/13 和 Gulp.js 3 上也遇到了错误。移至 Node.js 11 有效。

I solved by downgrading Node.js version from 12.14.0 to 10.18.0 and reinstalling node_modules .我通过将 Node.js 版本从12.14.0降级到10.18.0并重新安装node_modules来解决。

I was using Node.js v12.13.1, so I've downgraded to v10.19.0.我使用的是 Node.js v12.13.1,所以我已经降级到 v10.19.0。 It worked fine after that.之后它工作得很好。

Steps to fix the problem:解决问题的步骤:

I have fixed the problem with the following steps:我已通过以下步骤解决了问题:

  1. Installing NVM安装 NVM
  2. Installed lts/dubnium using command nvm install lts/dubnium使用命令nvm install lts/dubnium
  3. Use lts/dubnium using command nvm install lts/dubnium使用命令nvm install lts/dubnium

Now you can you gulp deploy.现在您可以部署 gulp。

For anyone that is running into this please confirm you haven't done the silly thing I did and accidentally ran npm init in your user directory a million years ago and forgot to clear out those files.对于遇到此问题的任何人,请确认您没有做过我做过的愚蠢的事情,并且在一百万年前不小心在您的用户目录中运行了npm init并忘记清除这些文件。

I found this issue when trying to use @vue-cli to create a new project and it ended up being a rogue package.json / package-lock.json file and node_modules folder in the root of my user directory.我在尝试使用@vue-cli创建一个新项目时发现了这个问题,它最终成为了一个流氓package.json / package-lock.json node_modules和我的模块用户文件夹的根目录。

Solution : Delete package.json , package-lock.json , and node_modules from your user directory and voila, problem solved, Now remove the palm from the forehead解决方案:从您的用户目录中删除package.jsonpackage-lock.jsonnode_modules ,瞧,问题解决了,现在从 forehead 中删除手掌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM