Search code examples
javascriptnode.jsnwjs

Compiling with Nwbuild ReferenceError: primordials is not defined


I made a simple app that uses fs and os as two packages in nodejs and used nwbuild to build the app. But it is giving me the following error:

fs.js:43
} = primordials;
    ^

ReferenceError: primordials is not defined
    at fs.js:43:5
    at req_ (/home/sahil/.nvm/versions/node/v20.9.0/lib/node_modules/node-webkit-builder/node_modules/natives/index.js:143:24)
    at Object.req [as require] (/home/sahil/.nvm/versions/node/v20.9.0/lib/node_modules/node-webkit-builder/node_modules/natives/index.js:55:10)
    at Object.<anonymous> (/home/sahil/.nvm/versions/node/v20.9.0/lib/node_modules/node-webkit-builder/node_modules/graceful-fs/fs.js:1:37)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)

Node.js v20.9.0

Versions:

Nodejs-v20.9.0

Yarn-1.22.21

nwjs sdk-v0.89.0

Please help!


Solution

  • The primordials variable is only available in ancient versions of Node.js. It's been removed for almost a decade now. It looks like you are using node-webkit-builder which was last published 9 years ago. You could use a 9 year old version of Node and NW.js to be compatible with node-webkit-builder, but it is deprecated, and no longer maintained.

    I would recommend switching to nw-builder which is officially maintained and has received constant updates for the past 2 years. Or making your own custom build script if you need greater control over the build process.

    It also looks like you are using nvm. I would recommend uninstalling nvm and replacing it with Volta, which allows defining the Node version in your package.json file, so that any time you cd into a directory you'll always be on the correct Node version automatically. There is also a base-volta-off-of-nwjs package that will update the Volta object to always match the version of Node built in to NW.js. It is important for you global Node version to match NW.js to avoid common issues. This approach automates that.