How to Define Node.js Versions in package.json

Did you know that our package.json file allows us to define Node.js versions in a project according to our comfort? If not, don’t worry! In this article, we will guide you through ways and steps on how you can define Node versions in your project according to your preferences using the ‘engines‘ field in your package.json file. We will try to cover the role of package.json, the ‘engines‘ field and how we define node version in there. Let’s commence!

Why We Need to Define a Node Version

It is important to understand what your project is about and how to use it. When you share your project, others can use your package.json to set up their environment quickly by running a single command (npm install). It is the control centre of your Node.js project as it manages dependencies, holds important project information, and simplifies project setup.

But what if the modules and package of the installed project file are not working well with the Node.js version installed? This can happen because that project file works best with a specific Node.js version. So to avoid errors due to such version mismatches, it is highly significant to mention or define the best-suited Node.js version for the project in the package.json file.

The ‘engines’ field in package.json comes in handy to do so, let’s understand it further.

Introduction to package.json File

This is one of the important files in Node.js projects. It is kind of a biodata for a project, which contains all the necessary important information about it such as dependencies and configuration settings.

Whenever you hit the following command:

npm init -y

You’ll see that a package.json file is created with some default information about versions, dependencies and configurations.

Package Json File Example

Understanding ‘engines’ Field

The ‘engines‘ field in a package.json file is used to specify the versions of Node.js and npm (Node Package Manager) that your project requires. In the package.json file, you can list all your node and npm versions under this section. Let’s see how are they specified.

Node.js Version:

  • “node”: “version” – It is used to specify the minimum version of Node.js required for your project.
  • “node”: “<=version” – It allows any version less than or equal to the specified version.
  • “node”: “>=version” – It allows any version greater than or equal to the specified version.

Similarly, for npm Version:

  • “npm”: “version” – It is used to specify the minimum version of npm required for your project.
  • “npm”: “<=version” – It allows any version less than or equal to the specified version.
  • “npm”: “>=version” – It allows any version greater than or equal to the specified version.

If you wish to know more about npm, continue reading – NodeJS NPM: A Beginner’s Guide to Node Package Manager

Steps to Define Node Versions in package.json

Let’s better understand this with an example.

STEP 1: Firstly, we will pick up a project on which we wish to specify Node.js versions. In that project, we will create a package.json file to work on, like the following:

{
  "version": "1.0.0",
  "engines": {
    
  },
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}

STEP 2: Here under the ‘engine’ section, we will write the following code:

"engines": {
"node": ">=14.0.0",  
"npm": ">=6.0.0"     
},

This code will ensure that your project runs only if your Node.js version matches the criteria of the engines field. If the current version does not match the specified version, an error will be thrown.

STEP 3: To make sure that a proper error is thrown if there is a mismatch in the version, we must add a .npmrc file. The “.npmrc “ is an INI (Initialization) file format used for the configuration of npm packages. It sets the rules and decides how npm should work.

So we create a file with an extension “.npmrc “ and add the following code in it:

engine-strict=true
Node version defined made sure

STEP 4: Now we use the command:

npm install

And we get the following error. This is because we use node version 10 and the requirement is version “>=14.0.0”.

Version mismatch error prove for node version defined

This helps avoid problems because different versions of Node.js and npm might have different features or ways of doing things. Setting these rules ensures everyone is on the same page and can run the project without issues.

If the ‘engines’ field is not present in your package.json file, it means that your project does not have any specific Node.js version requirements.

Also if you want to know how we effortlessly switched node versions here, check out – How to Downgrade Node Version: 3 Easy Ways

Summary

That’s it! We hope that now you are clear on how to define Node versions in the package.json file. In short, it’s best to specify Node.js versions in the ‘engines’ field so that during installation of the project by your fellow developer, the system checks for the Node.js versions that have the best chemistry with the project.

Reference

https://stackoverflow.com/questions/29349684/how-can-i-specify-the-required-node-js-version-in-package-json

Snigdha Keshariya
Snigdha Keshariya
Articles: 98