Node versions
Created Wed Dec 27, 2023 at 12:38 AM
Many projects have bad documentation or ways around what Node.js version they use, and how to load it.
But there's a simple explicit way to do both enforcement and convenience at once.
It involves making changes to 4 files - .nvmrc, .npmrc, package.json and terminal hook (.zshrc for example).
- Enforcement - the app will raise errors for wrong versions during
npm install..npmrcandpackage.jsondo this, since any app is started by an npm script. This raises an error duringnpm installitself for wrong Node version, which is a good thing. See StackOverflow
/.npmrc
engine-strict=true
/package.json. See engines property on npmjs
"engines": {
"node": "<=16"
},
// supports range like so `>=0.10.3 <15`
// optionally, even `npm` version can be specified
-
Convenience
- Manual load - Add
.nvmrcwith the node version (by runningnode -v > .nvmrc), then manually donvm useinside the directory. Don't have to remember "x" innvm use x, good.
/.nvmrcand (for safety/other environments)/.node-versionfile
v16.20.2- Auto load - remembering and doing
nvm usefor each new terminal is lame. So, add.nvmrcto the project as usual, and also add a function that loads the node version. Fully automatic. See Deeper shell integration
~/.zshrc
# for ZSH: Add to .zshrc https://github.com/nvm-sh/nvm?tab=readme-ov-file#zsh # for bash: Add to .bashrc https://github.com/nvm-sh/nvm?tab=readme-ov-file#bash # optionally can do - Manual load - Add
Notes and gotchas:
- Set global default:
nvmhas a bad UI. To set global default, one has to runnvm use alias default _version_ - Use specific node version (temporarily - for terminal session) -
nvm use _version_ - On mac, make sure to run
brew uninstall node. Homebrew node messes up with nvm global default - it just takes over the default.
PS
Time to take asdf seriously?
Top comments (0)