How to Fix ReferenceError: primordials is not defined error in Node.js

ReferenceError: primordials is not defined error occurs in Node.js when you are “using older versions of certain Node.js modules like gulp with newer versions of Node.js, specifically Node.js v12 and later.”

The main reason for the error is that gulp v3 doesn’t work (as of now) under node v12 because it depends on graceful-fs@^3.0.0, which patches Node’s fs module, and that patch worked before node v12 just fine.

The primordials object, which was previously present in Node.js internals, was removed in Node.js v12. Some older packages depend on modules that reference this primordials object, which is why they break when used with newer versions of Node.js.

How to fix the ReferenceError: primordials is not defined error

Here are three ways to fix the ReferenceError: primordials is not defined error.

  1. Upgrade the gulp.js version to 4.0.2
  2. Downgrade Node.js version to 11
  3. Pin graceful-fs to version 4.2.2 using an npm-shrinkwrap.json file.

Solution 1: Upgrade the gulp.js version to 4.0.2

To upgrade gulp.js to the latest version, use this command:

npm install gulp@latest

Solution 2: Downgrade Node.js version to 11

You can switch to an older version of Node.js, where the primordials module is still defined. This can be done quickly using nvm (Node Version Manager) tools. For example, you can switch to Node.js v11.

nvm install 11

nvm use 11

Solution 3: Pin graceful-fs to version 4.2.2 using an npm-shrinkwrap.json file.

To pin graceful-fs to version 4.2.2 using an npm-shrinkwrap.json file, follow these steps:

Step 1: Ensure you have graceful-fs installed

npm install graceful-fs@4.2.2

Step 2: Generate an npm-shrinkwrap.json file

If you don’t already have an npm-shrinkwrap.json file in your project, you can create one by running:

npm shrinkwrap

This command will generate an npm-shrinkwrap.json file based on the current state of the node_modules directory and your package.json file.

Step 3: Edit the npm-shrinkwrap.json file

Open the generated npm-shrinkwrap.json file in a text editor and locate the graceful-fs dependency. Manually set its version to 4.2.2.

"graceful-fs": {
  "version": "4.2.2",
  ...
}

Step 4: Lock Down the Dependency

With the npm-shrinkwrap.json file in place, the versions of the dependencies specified in the file will take precedence over those in the package.json file.

Remember, whenever you add or update a package in your project, you should update the npm-shrinkwrap.json file as well to ensure that the locked versions are up to date.

And it will fix the error!

Leave a Comment