When developing web applications, always pay attention to the versions being supported, particularly the versions of node and npm. This is because running apps or installing packages on unsupported versions may lead to unexpected errors!
Below, we’ll explore how to set up versions of node and npm in package.json
to address these issues.
# How to Set Node and NPM Versions in package.json
Version setting is typically done by specifying a range, like from v1 to v2. To set up the versions, you can add "engines". Within "engines", you can set different versions for "node" and "npm". The example below shows how to set it up for using node versions higher than or equal to v16 but less than v17. Additionally, it declares to set up npm versions higher than or equal to v8 but less than or equal to v9.@ package.json
"node": ">=16.0.0 <17.0.0",
"npm": ">=8.0.0 <9.0.0"
With that, the setup is complete. To reiterate, the configured package.json as above means that:
- Node version is equal to or higher than 16 but lower than 17
- NPM version is equal to or higher than 8 but lower than 9
We have simply set up versions in package.json like this.
Note that setting it up this way does not mean that the script in package.json won’t run. In other words, even if you run npm start to launch the app, it won’t necessarily stop if the versions are out of the specified range. Thus, it serves to notify of the versions set in package.json. If it’s not within the allowed version range, you should use tools like nvm to install and use versions within the specified range![ Reference ]
For older npm versions, prior to v3.0.0, there used to be a way to enforce it with engineStrict: true
, but this does not work in later versions. Please take note of this.
So far, we’ve briefly looked at how to set up node and npm versions in package.json.