How to fix ReferenceError primordials is not defined error

When use gulp in an old project, It outputs an error:

1
2
3
4
5
6
fs.js:45
} = primordials;
^

ReferenceError: primordials is not defined
// ...

And how to fix this error?

It occurs when use gulp 3.x with node upper than 12.x,because gulp 3.x depends on graceful-fs@^3.0.0 which patches Node’s fs module and that patch worked well before node v12.

Solutions

There are some solutions:

  1. upgarde gulp
  2. downgrade node
  3. to lock graceful-fs to version 4.2.2

For me, i do not want to re-write and re-config my project toolchain, and i also cannot change node version. So lock the gracefull-fs is good for me. The concrete solution here:

  1. create publishable lockfile npm-shrinkwrap.json
  2. edit the npm-shrinkwrap.json:
1
2
3
4
5
6
7
{
"dependencies": {
"graceful-fs": {
"version": "4.2.2"
}
}
}
  1. re-run npm install

Now, it should be working just fine. Enjoy it!