Engine Locking in Node Project

It is common to encounter compatibility issues when working with Node.js projects. If the project is initialized with one version of Node.js and a team member uses a different version, it can cause issues. To avoid this inconvenience, It is recommended that all developers working on the project use the same Node engine and package manager. To accomplish this, we create two new files:

  • .nvmrc: This file specifies which version of Node.js is used.

  • .npmrc: This file specifies which package manager is used.

If you are using Node.js v18 Hydrogen and yarn for the project, you can set the values as follows:

In the .nvmrc file, specify:

lts/Hydrogen

In the .npmrc file, specify:

engine-strict=true

You can find a collection of Node.js names and their corresponding versions here: github.com/nodejs/Release/blob/main/CODENAM..

You might be wondering what "engine-strict=true" does in the .npmrc file. This flag ensures that the correct version of Node.js is used for the project.

As for yarn, we specify it in the package.json file as follows:

{
  "name": "Test Project",
  "author": "Imtiazpy",
  "description": "Description",
  "version": "0.1.0",
  "private": true,
  //Engine locking here
  "engines": {
    "node": ">=18.0.0",
    "yarn": ">=1.22.0",
    "npm": "please-use-yarn"
  },
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
}

The "engines" field is where you specify the specific versions of the tools you are using. You can also fill in your personal details if you choose.