How to Lock Node.js Project Version

2022/9/102 min read
bookmark this
Responsive image

About this article

If you have multiple Node.js project on your local development environment, and if each of them is using Node.js version, you might get error when use the wrong Node.js version for the project.

So this article is provide a way to lock the Node.js version for the particular project to avoid is kind of error. It won't be fixed for all the scenarios, but at lease follow the steps can avoid for certain scenarios.

So follow list of steps we'll do for avoid this.

  • add engines to specify Node.js Version.
  • add .npmrc for strict mode.

Add engines at package.json to specify the version

The below specify the Node.js project need to be running version 14 or above, but just add the version at package.json nothing will happen.

{
  "engines": {
    "node": ">=14.x"
  }
}

Create .npmrc and set the engine-strict=true

Once create the .npmrc and set the engine-strict=true then the dependencies packages will be lock with the version, so if you set the version as 14.x, but your local's current Node.js is 12.x, then it will get error when running the npm install.

So this will prevent the project is expect to be Node.js version 14, but somehow developer's local is running Node.js as version 12 and trying to load the Project, this will force the developer to use the correct Node.js version 14 for tha particular project.