How to specify the required Node.js and npm version in package.json?

In this tutorial we will learn how to lock the required Nodejs and npm version or rather specify the Nodejs and npm version so everyone working on project uses same version to avoid any kind issues due to version mismatch.

Using package.json engine property


By default Nodejs’s package.json provides engine property to define supported version for both nodejs and package managers such as npm, yarn and pnpm.

{
 "engines" : { 
    "npm" : ">=8.0.0 <9.0.0",
    "node" : ">=15.0.0 <16.0.0"
  }
}

more @ https://docs.npmjs.com/cli/v9/configuring-npm/package-json#engines

To enforce this version we need to add engine-strict property in .npmrc file in your project.

# .npmrc
engine-strict=true
How to enforce this engine versions?

Using Node.js version managers


we can also use Node version managers such as nvm to and add node version in .nvmrc and nvm use will automatically use the specified node version and if you zsh or oh-my-zsh there are plugins to do this automatically when you go to this project dir.

nvm install 18
nvm use 18

node --version > .nvmrc

nvm use

Pro Tip:

use nvm auto switch script specified here based on your shell preference.

https://stackoverflow.com/questions/23556330/run-nvm-use-automatically-every-time-theres-a-nvmrc-file-on-the-directory

See also  Fix node is not recognized as an internal or external command Error

Leave a Comment