32

Is there a way besides trial and error to detect what node version I should use on a repository ?

With the fast rise of web frameworks it's becoming a common need to go back to projects from 6, 12 or 24+ months ago. I've been doing this a lot in the last weeks and my process for detecting the correct node version has become:

git clone [REPO]
npm i
[build]
### if error
rm -r node_modules
nvm use 4, 5 or 6
npm i
[build]

I can't help but feel that there's something very basic I'm missing here. Thanks for any wisdom you can share with me!

coiso
  • 7,151
  • 12
  • 44
  • 63

8 Answers8

20

As mentioned in other answers, some packages have an engines field in the package.json. npm will warn users if they try to install a package on a Node.js version not supported by the package. The engines field looks like this:

{
  "engines": {
    "node": ">=4.0.0"
  }
}

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


Also, many packages have a CI, set up to automatically test packages. These CI config files often contain a list of Node.js versions to test on. Generally, the lowest version number in the CI config is the minimum version supported by the package.

Common CI config files are .travis.yml, appveyor.yml, circle.yml, or any files in .github/workflows.

Example:

.travis.yml:

language: node_js
node_js:
  - "4"
  - "5"
  - "6"
  - "7"

This config means that the package probably supports Node.js v4+

RyanZim
  • 6,609
  • 1
  • 27
  • 43
9

If the node engine parameter is not specified by the developer in package.json you can try looking at the same parameter in the package.json of your node modules. Chances are if your using common well support modules it will be defined. Running the following command will give you a semi sorted version list to go through.

grep -hoP '"node":.*' node_modules/*/package.json | sort

Usually the module that requires the highest version of node is determining factor in what version of node you'll need to for the project to work.

wizzfizz94
  • 1,288
  • 15
  • 20
  • I like this one! Even tried to convert it to Select-String for Windows but alas, I had just cloned the repo so no node_modules folder...Here's my start to the Select-String that I never got to use if anyone wants to enhance it: Select-String -Path "node_modules/*/package.json" -Pattern '"node":.*' – sigmapi13 Aug 25 '23 at 15:08
8

The project might have a .nvmrc file containing the valid version(s) for it.

anothernode
  • 5,100
  • 13
  • 43
  • 62
6

If these are your own repos, then you can store the Node version in package json for your reference.

"version": "1.0.0",
"engines": {
    "node": "7.x"
},
"description": "..."

This won't automatically setup the correct version, but it would provide you with somewhere to look.

Varedis
  • 738
  • 5
  • 14
  • 1
    This is a great way, and very useful in combination with dev ops tech (docker, kubernetes, etc.) but the problem is that many repos on github with useful examples and even internal projects don't have the node version specified this way :s – coiso Mar 15 '17 at 10:25
3

if you do need to trial-and-error, you can speed up the process with npm's node combined with npx

npx node@4 myscript.js

there's even a shell autofallback option you can setup so just

node@4 myscript.js
tonywoode
  • 166
  • 1
  • 6
2

currently, numbers of versions has become overwhelming with the fast release cycles.

A better solution than trial/error is to look at the dates of files/folders a project uses and search the release dates of nodejs versions in the following link:

https://nodejs.org/en/download/releases/

in the search box, start with a space then the first 1-2 numbers of a release and check the date of release. 14 (note the space) will show releases from Node.js 14.xx.xx.

Though it is not the perfect solution as the project might still be written with an even older version, it will at least show a compatible version that was out at that time.

Yılmaz Durmaz
  • 2,374
  • 12
  • 26
0

If you are using yarn, you can use yarn why node or yarn why @types/node.

0

I found the Node version in the package-lock.json file, at:

"engines": {
    "node": ">= 10.13.0",
    "npm": ">= 6.11.0",
    "yarn": ">= 1.13.0"
}