Find which node version an old project requires
Introduction
If a project doesn’t specify which version of node was used when created it can be problematic as time passes and you need to do some further work down the line.
You can check in package.json for an engines reference e.g;
{
"engines": {
"node": ">=8.1.0"
}
}
You can also check for a .nvmrc file or as a last resort a quick scan of the readme… Good luck!
Yeah, I usually don’t find it either
In almost all cases when working with other peoples projects I’ve found that the version of node they used to compile assets etc wasn’t recorded. This means you’re left in a pickle as it’s unlikely a recent version will play nicely with old modules after any length of time…
Grep to the rescue
The snippet below will work on an Apple Mac’s Terminal software, check the appropriate help files if you encounter an ‘invalid option‘ error.
grep -hop '"node":.*' node_modules/*/package.json | sort
Run it after cd‘ing into the projects root, same level as the node_modules directory. You’ll see a list of all the versions that’ve been used, image below shows a small sample.
Scan your eye over the whole list (it may be quite long) keeping a mental note of the highest version you’ve seen, that’s the version you’ll need and can install with nvm to get you up and running.
What do those flags do?
Just in case you were wondering, the -hop options are as follows;
- -h
Suppress prefixing of filenames on output when multiple files are searched. - -o
Show only the part of a matching line that matchesPATTERN. - -p
No symbolic links are followed.
Final thoughts
Now (hopefully) that you’ve found the version you need, remember to add it to a .nvmrc file or as an engines reference in package.json – you’ll thank me when you do!
Whilst this is isn’t the fastest, cleanest way to do things, it at least gets the job done. If you know of any better methods please let me know, cheers.
// End of Article
Article Information
Further Reading
- Grep Background Info (wikipedia.org)
- Node JS Home (nodejs.org)
- NVM Documentation (github.com)