by Sai gowtham

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

  1. Open the node project in your favorite code editor.
  2. Navigate to the package.json file and add the following engines 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.

  1. Create a .npmrc file in your project root folder and the following config.
.npmrc
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:

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course