FUNDAMENTAL

How to set the node version in package.json?

If you have ever worked with the nodejs project, you might have encountered the package.json file. The package.json is the file which contains the configuration related to that project.

The package.json is a JSON file with a bunch of key-value pairs.

A sample package.json file is:

{
"name": "timer",
"version": "0.1.0",
"private": true,
"dependencies": {
"@reduxjs/toolkit": "^1.8.1",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.0.1",
"@testing-library/user-event": "^14.1.1",
"@types/jest": "^27.4.1",
"@types/node": "^17.0.25",
"@types/react": "^18.0.6",
"@types/react-dom": "^18.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.0.1",
"react-scripts": "5.0.1",
"typescript": "^4.6.0",
"web-vitals": "^2.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"engines": {
"node": "15.14.0"
}
}

As you can see above, the package.json has different - 2 key-value pairs.

If you want to set the node engine version to be used by the project, you can do so by specifying the node-version value in the node key inside engines key.

"engines": {
"node": "15.14.0"
}

From the above code, we can interpret that project is going to strictly use 15.14.0 version of node.

The version value follows the semver (semantic versioning) specification. Read this article on semantic versioning if you want to know more.

If you want to use any version of node, just specify Asterisk(*) as its value

For e.g.:

"engines": {
"node": "*"
}

or if you remove the node key in engines, then also the project will take any version of the node.

That's all

I hope that this article would have been useful to you. Keep coding and keep solving problems.