1

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?

koalaok
  • 5,075
  • 11
  • 47
  • 91

1 Answers1

1

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...")
h-sifat
  • 1,455
  • 3
  • 19
  • Thanks for your answer. But my question was not about how to implement it via code, but about package.json engines directive, why it doesn't work... and if it's not supposed to work in this case by design. – koalaok Oct 08 '21 at 08:39
  • @koalaok sorry about that. In that case I don't know the answer :( – h-sifat Oct 08 '21 at 08:45