Specifying a required Node.js version in Package.json file
In this tutorial, we are going to learn about how to specify a required node version inside your package.json file.
If we specify the required node.js version in package.json
file npm will notify the users that our project only runs on a particular node version.
Specifying the Node version
-
Open the node project in your favorite code editor.
-
Navigate to the
package.json
file and add the followingengines
field by replacing it with your required node version.
"engines": {
"node": ">=12"
},
Here I set a node version >=12
so that my project only works on node version 12 and above.
- Create a
.npmrc
file in your project root folder and the following config.
engine-strict=true
This will give an error to the user if he or she uses different node versions (other than specified).
npm ERR! code ENOTSUP
npm ERR! notsup Unsupported engine for node-app@1.0.0:
wanted: {"node":">=8"} (current: {"node":"10.14.1","npm":"6.13.4"})
npm ERR! notsup Not compatible with your version of node/npm: node-app@1.0.0
npm ERR! notsup Not compatible with your version of node/npm: node-app@1.0.0
npm ERR! notsup Required: {"node":">=12"}
npm ERR! notsup Actual: {"npm":"6.13.4","node":"10.14.1"}
npm ERR! A complete log of this run can be found in: