How to alert user he/she does not have the correct Nodejs engine when running my app?

Viewed 48

I'm developing and application with Node >=14

I'd like that the application wont start or at least provide a warning message: if the nodejs engine installed on the host environment is not matching the required version.

I know I can possibly achieve it via code, at bootstrap.

I wonder if using the package json engines directive it is possible to obtain this result

...
"engines": {
    "node": "^14"
  },
...

Actually the above is not considered if I switch on my host for example to node 12

And if I run

npm run start

it will work without providing any feedback or error.

To emulate different nodejs engine version I'm using n package

    node/12.18.3
    node/13.1.0
    node/14.17.0
  ο node/14.18.0

Is it possible or the engines directive is going to be fired only on node modules install to check intra package dependencies?

1 Answers

If setting the Node version in package.json file doesn't work then I think you've to alert the user manually.

You can get the current node version from the process.version property.

So you'd have to do something like this:

const currentMajorVersion = +process.version.slice(1).split(".")[0];
// e.g., "v14.10.1" => 14

if(currentMajorVersion < whatever)
    console.log("Warning...")
Related