# node.js – How to fix ReferenceError: primordials is not defined in node

## The Question :

527 people think this question is useful

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



652 people think this answer is useful

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

A previous workaround from Jan. does not work either: https://github.com/gulpjs/gulp/issues/2246

Solution: Either upgrade to gulp 4 or downgrade to an earlier node.

473 people think this answer is useful

We encountered the same issue when updating a legacy project depending on gulp@3.9.1 to 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.4.

## If you are using yarn v1

Yarn v1 supports resolving a package to a defined version. You need to add a resolutions section to your package.json:

{
"resolutions": {
"graceful-fs": "^4.2.4"
}
}



Thanks @jazd for this way to solve the issue.

## If you are using npm

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:

{
"scripts": {
"preinstall": "npx npm-force-resolutions"
},
"resolutions": {
"graceful-fs": "^4.2.4"
}
}



npm-force-resolutions will alter the package-lock.json file to set graceful-fsto the wanted version before the install is done.

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.

## 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.

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 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).

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.

210 people think this answer is useful

# Fix it in 1 minute:

Just follow these steps. I’m on windows 10 and it worked perfectly for me!

1. In the same directory where you have package.json create a npm-shrinkwrap.json file with the following contents:
    {
"dependencies": {
"graceful-fs": {
"version": "4.2.2"
}
}
}


1. Run npm install, and don’t worry, it’ll update npm-shrinkwrap.json with a bunch of content.

2. Run gulp to start the project.

105 people think this answer is useful

Use following commands and install node v11.15.0:

npm install -g n

sudo n 11.15.0



will solve

ReferenceError: primordials is not defined in node

Referred from @Terje Norderhaug @Tom Corelis answers.

36 people think this answer is useful

Use following commands to install node v11.15.0 and 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



23 people think this answer is useful

Using NVM to manage what node version you’re using, running the following commands worked for me:

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



19 people think this answer is useful

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:

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



13 people think this answer is useful

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.

In the same directory where you have package.json create a npm-shrinkwrap.json file with the following contents:

{
"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. Let’s get rid of this updates by updating package.json scripts options.

"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.

12 people think this answer is useful

had same error and finally fix that when updated all packages and then mentioned the same node 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.

for more checkout heroku support

9 people think this answer is useful

# TL:DR

Gulp 3.* doesn’t work on Node 12.* or above. You have to downgrade Node, or upgrade 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!

As others have already mentioned, Gulp 3.* is not supported on Node 12 or above, so you will have to downgrade your Node version to 11.* or below, OR upgrade your Gulp to 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.

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 versions.

### Installing Node version on N

n 10.16.0



### InstallingNode version on NVM

nvm install 10.16.0



One 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 re-installing your dependencies. Though if you are merely reverting to a pre-existing Node version, you should probably be fine.

As mentioned above, this is a more time-intensive task, but might bring benefits in the long-run. For example, Node 12 has now introduced native support for ES Modules (behind an experimental flag) and full support in 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.

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.

## But I already have Gulp 4, and it still doesn’t work!

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.

In my case, gulp-combine-mq was a dependency using Gulp 3.9.*. Disabling this task in my gulpfile allowed Gulp to run again.

If this happens, you have a few options: you can,

1. Go without the plugin if it’s not absolutely necessary
2. Find an alternative,
3. 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).

If this happens, it is best to just downgrade Node, at least until patches can be issued.

8 people think this answer is useful

In case the problem is not from gulp then check the unzip npm module. it’s been around six years since the last time it was updated. it doesn’t work with Node > v11.

Try this:

npm install -g n

sudo n 11.15.0



4 people think this answer is useful

Downgrading to node stable fixed this issue for me, as it occurred after I upgraded to node 12

sudo n 10.16.0

4 people think this answer is useful

Check node version:

 node --version



Check gulp version:

gulp -v



If node >=12 and gulp <= 3, do one of the following:

sudo npm install -g gulp


sudo npm install -g n
sudo n 11.15.0



3 people think this answer is useful

This error is because of the new version of Node (12) and an old version of gulp (less than 4).

Downgrading Node and other dependencies isn’t recommended. I solved this by updating package.json file fetching latest version of all dependencies. For this, I use npm-check-updates. It is a module that updates the package.json with the latest version of all dependencies.

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:

Before:

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

....

}
);

Other config...

'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...

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



2 people think this answer is useful

I was getting this error on Windows 10. Turned out to be a corrupted roaming profile.

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.

2 people think this answer is useful

Upgrade to 4.0.1 and make sure to migrate https://fettblog.eu/gulp-4-parallel-and-series/#migration

2 people think this answer is useful

I fixed this issue on Windows 10 by uninstalling node from Add or remove programs -> Node.js

Choose node-v11.15.0-x64.msi if your running windows 64bit.

2 people think this answer is useful

I faced the same issue. What I tried and worked for me:

1. Check the version of NODE and GULP (combination of node v12 and gulp less than v4 doesn’t work)

2. I downgrade NPM version by:

• sudo NPM install -g n
• sudo n 10.16.0

1 people think this answer is useful

This might have come in late but for anyone still interested in keeping their Node v12 while using the latest gulp ^4.0, follow these steps:

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

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



Delete your package-lock.json file

Delete your node_modules folder

Finally, Run npm i to upgrade and recreate brand new node_modules folder and package-lock.json file with correct parameters for 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.

To learn more about applying these new features, this site have really done justice to it: https://www.sitepoint.com/how-to-migrate-to-gulp-4/

(If it helps, please leave a thumps up)

1 people think this answer is useful

Gulp is making issue with Nodejs version 11 and above. Uninstall your current node version and reinstall the v10.15.1 here is link for that version. This helps me and it will solve your problem too.

0 people think this answer is useful

For those who are using yarn.

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



0 people think this answer is useful

This is because the compatibility issue between node and gulp in your system. Downgrading the node or upgrading the gulp will fix this issue.

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.

0 people think this answer is useful

If you’re trying to install semantic-ui and the following error occurs then try downloading the latest version of node js(13.5.0) with the latest features, from Node.js.org, Moreover rather than trying NPM install semantic you should just add the link (which you can find from cdnjs link to the header of your index.html file. Best of luck!

-1 people think this answer is useful

I Hit this error after upgrading my node to version 12, which doesn’t work with Gulp 3.9.1. Regarding the fact that my gulpfile.js wasn’t so complex, I decided to upgrade to Gulp 4 using this article, It went well, and is was much easier than I thought.

-1 people think this answer is useful

You Have Two Option Here

1. Either upgrade to gulp 4 Or Else
2. downgrade to an earlier node version.

-1 people think this answer is useful

I was using node v12.13.1, so I’ve downgraded to v10.19.0 and works fine after that.

-1 people think this answer is useful

I got the same issue installing the npm package https://www.npmjs.com/package/webshot

NOTE: it was a known issue for that package as it depends on graceful-fs behind the scenes.

FIX:1. upgrade graceful-fs to 4.x or higher

-1 people think this answer is useful

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;

• Uninstall Node.js from “Add or remove programs”. Fire up a new Command prompt and type gulp -v and then node -v to check that it has been uninstalled completely.
• 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. During installation I didn’t change the installation path which I normally do(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.

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.

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.

-2 people think this answer is useful

For anyone having same error for the same reason in ADOS CI Build:

This question was the first I found when looking for help. I have an ADOS CI build pipeline where first Node.js tool installer task is used to install Node. Then npm task is used to install gulp (npm install -g gulp). Then the following Gulp task runs default-task from gulpfile.js. 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 version was 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. Dsouza and 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 version from 11.x.

The exact version of Node.js that got installed and is working was 11.15.0. I didn’t have to downgrade the Gulp.

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