I have a Node.js project that requires Node version 12 or higher. Is there a way to specify this in the packages.json file, so that the installer will automatically check and inform the users if they need to upgrade?
You can set the engines field in your package.json and set version requirements for node, npm or both:
"engines" : {
"npm" : ">=8.0.0 <9.0.0",
"node" : ">=16.0.0 <17.0.0"
}
As your code definitely won't work with any lower versions, you can enforce this via npm. You need to create an .npmrc file, commit it to the repository, and set the engine-strict option to true, which will cause npm commands such as npm install to fail if the required engine versions to not match:
# .npmrc
engine-strict=true
Without that file, every developer will need to run npm config set engine-strict true in their local workspace to switch on this option.
You'll want to set the "engineStrict" flag in your package.json:
{ "engineStrict" : true }
Note that the engineStrict setting in package.json is deprecated: As of npm 3 it will only give a warning. You will have to either configure the .npmrc file as above, or each user will have to run npm config set engine-strict true if they want npm to enforce the required engine versions.
Documentation for the package.json file can be found on the npmjs site