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
..npmrc
andpackage.json
do this, since any app is started by an npm script. This raises an error duringnpm install
itself 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
.nvmrc
with the node version (by runningnode -v > .nvmrc
), then manually donvm use
inside the directory. Don't have to remember "x" innvm use x
, good.
/.nvmrc
and (for safety/other environments)/.node-version
file
v16.20.2
- Auto load - remembering and doing
nvm use
for each new terminal is lame. So, add.nvmrc
to 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:
nvm
has 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)