How to check minimum required Node.Js version

Adam Bisek
2 min readJan 12, 2017

--

For many apps may be useful to check the Node.Js engine version and require specific version.

We can use “engines” section in package.json that is intended for purposes like this.

{
"name": "my package",
"engines": {
"node": ">=50.9" // intentionally so big version number
},
"engineStrict": true
}

The “engineStrict” field should be the way to require versions described in “engines”, but, unfortunately, as of npm 3 is this field deprecated. I didn't find any way how to do that natively with npm.

So I am going to share with you my solution. Don't be afraid, it is really simple. Note: I'll be using ES6 syntax and assuming Babel transpiler.

npm install --save semver

Save it as a file named “check-version.js”. Good, now we need to run this check before every installation.

Let's put it into “scripts” section in package.json. The “postinstall” is the script we want. This is how will look like whole package.json:

{
"name": "my package",
"engines": {
"node": ">=50.9" // intentionally so big version number
},
"scripts": {
"requirements-check": "babel-node check-version.js",
"postinstall": "npm run requirements-check"
}
}

That's all folks :) Let's try it:

It works! Now just set version you want to in “engines” section - in my case I've set “≥6.9”.

EDIT: Current version of yarn (1.x) is now (2018) respecting engines field as expected, so if you are using yarn, you are fine. And according to documentation, npm 5.x is also aware of engines field too.

--

--