ReferenceError: primordials is not defined when running gulp with Node.js version 12 and above

Written by - 0 comments

Published on October 6th 2021 - Listed in Coding

Whilst trying to manually compile a program from source code, one of the steps is to use gulp to concatenate CSS and JavaScript files.

However when I tried this, I ran into the following error:

ck@mintp ~/Git/app $ gulp -f gulpfile.js
ReferenceError: primordials is not defined
    at fs.js:45:5
    at req_ (/home/ckadm/Git/invoiceninja/node_modules/natives/index.js:143:24)
    at Object.req [as require] (/home/ckadm/Git/invoiceninja/node_modules/natives/index.js:55:10)
    at Object.<anonymous> (/home/ckadm/Git/invoiceninja/node_modules/vinyl-fs/node_modules/graceful-fs/fs.js:1:37)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)

According to this Stackoverflow question, the problem is caused by an incompatibility between gulp 3.x and newer Node.js versions.

The gulp command above was run with gulp-cli 2.3.0 and gulp 3.9.1 in the application directory:

ck@mintp ~/Git/app $ gulp --version
CLI version: 2.3.0
Local version: 3.9.1

Node.js at this point was installed with 14.15.4:

ck@mintp ~/Git/app $ node -v

The same Stackoverflow question mentions to either upgrade to gulp 4.x or downgrade node.js to version 11.

I decided to do the latter and be able to switch between node.js versions in the future. node.js 11.15.0 was downloaded, unpacked and the binary copied to /usr/local/bin/node-11.15.0. Then the previous alternatives configuration (pointing to node 14.15.4) was removed. And finally, the new alternatives link was set by using update-alternatives --install, pointing to /usr/local/bin/node-11.15.0:

ck@mintp ~/tmp $ wget
ck@mintp ~/tmp $ tar -xzf node-v11.15.0-linux-x64.tar.gz
ck@mintp ~/tmp $ cd node-v11.15.0-linux-x64/
ck@mintp ~/tmp/node-v11.15.0-linux-x64 $ sudo cp -p bin/node /usr/local/bin/node-11.15.0
ck@mintp ~/tmp/node-v11.15.0-linux-x64 $ sudo update-alternatives --remove node /usr/local/bin/node
ck@mintp ~/tmp/node-v11.15.0-linux-x64 $ sudo update-alternatives --install /usr/bin/node node /usr/local/bin/node-11.15.0 1 --force
ck@mintp ~ $ node --version

Note: Also see older article Installing a newer node.js and npm version on Debian 10 (Buster) using update-alternatives.

With Node.js running as version 11.15.0, the gulp command executed successfully:

ck@mintp ~/Git/app $ gulp -f gulpfile.js
[11:34:40] Using gulpfile ~/Git/app/gulpfile.js
[11:34:40] Starting 'all'...
[11:34:40] Starting 'styles'...
[11:34:41] Finished 'styles' after 90 ms
[11:34:41] Starting 'styles'...
[11:34:41] Finished 'styles' after 37 ms
[11:34:41] Starting 'styles'...
[11:34:41] Finished 'styles' after 12 ms
[11:34:41] Starting 'styles'...
[11:34:41] Finished 'styles' after 4.02 ms
[11:34:41] Starting 'styles'...
[11:34:41] Finished 'styles' after 11 ms
[11:34:41] Starting 'styles'...
[11:34:41] Finished 'styles' after 3.67 ms
[11:34:41] Starting 'styles'...
[11:34:41] Finished 'default' after 9.16 ms

Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.