惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
I
InfoQ
B
Blog
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
量子位
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
小众软件
小众软件
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
美团技术团队
人人都是产品经理
人人都是产品经理
博客园 - 三生石上(FineUI控件)
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
博客园 - 聂微东
IT之家
IT之家

Ben Frain

Well done AI, that’s the end of the honest boxes The Beginnings, by Rudyard Kipling So, you want a React modal that uses the <dialog> element and transitions in AND out? Scroll indicators on tables with background colours using animation-timeline Review: SoundPEATS Clip1 Open ear clip-on headphones VS Code – highlight just the active indent guide Review: MoErgo Go60, a split ergonomic and fully programmable keyboard Review: Kinesis mWave mechanical ergonomic and programmable keyboard iOS26 Safari theme-color/tab-tinting with fixed position elements is a mess New Book: Responsive Web Design with HTML5 and CSS, 5th Edition Use @supports with a proxy feature/value for features you can’t test for (@starting-style) First adventures in View Transitions Review: Benq Screenbar Pro and Halo lightbars. The kit you never knew you needed! Center items in a container, and make then left aligned when they overflow A single element CSS donut timer/countdown timer, that can sit on any background Review: Open Ear Headphones – Bose Open Ultra v Huawei FreeClip In search of the perfect autocomplete for CSS Review: Keychron Q14 Max Alice 96 Key mechanical keyboard NEW VIDEO COURSE: Responsive Web Design with HTML5 and CSS Is CSS Grid really slower than Flexbox? Review: Advantage360 Pro Signature Edition 2024 mechanical ergonomic keyboard More Keys or Fewer Keys for mechanical keyboards Yes! You can use position: sticky and overflow together Neovim – how to do project-wide find and replace? Review: Keyboardio Model 100, split, wooden, mechanical keyboard Struggling to learn SwiftUI How to create rounded gradient borders with any background in CSS How to get equal size icons in the cmp completion menu of Neovim with Kitty terminal Review: Dygma Defy, split, mechanical, programmable ergonomic keyboard What’s the best way to reset WAAPI chained animations?
Managing multiple versions of node, without NVM or additi...
Ben Frain · 2024-10-21 · via Ben Frain

So, you are in a situation where you have to use specific versions of Node, but don’t have access to NVM or N.

Its a pain to do the clicky-clicky installer everytime and re-install. You want a quicker way of loading the relevant version.

The crux of this approach is installing two or more different binaries of Node into different folders on your system and then using an alias in your shell to update the path so you can swap between them. A way to point your system to different versions if you will. Another dev (Hi Sam!) gave me the idea and I’m just writing out the detail here for future reference.

Practically, you will be able to enter nr in your Terminal to switch to Node v22.9.00 for your Node React project, or ns to switch to an older version of Node to support an older Svelte project that needed v10.9.3, or whatever. Obviously the names, commands, and versions will be particular to you and your projects.

I’m on macOS here so imagine Linux will be similar, not sure on Windows, sorry.

Download the binaries

Head over to https://nodejs.org/dist/ and click on the first version of Node you are interested in. For example, I wanted v22.9.0 so went to https://nodejs.org/dist/v22.9.0/. Then you want to download the relevant binary so get the *.gz file that matches your system. I’m on macOS Apple Silicon, so downloaded https://nodejs.org/dist/v22.9.0/node-v22.9.0-darwin-arm64.tar.gz.

Note, you don’t want the download that includes the source code. When you have extracted the file you download, in the next step, when you extract the contents, there should be a bin folder. If not, you have likely downloaded the source code by mistake

Now you need to repeat that process for each version you need. For the sake of explanation I have also downloaded node-v10.15.3-darwin-x64.tar.gz.

Move the gz files somewhere practical

I wanted my two (or more) versions of Node stored in folders out of the way but somewhere sensible, so put them within ./local/share/, so like my v10.15.3 would be in ./local/share/node101503.

So I moved my downloaded gz files into that share folder ready for the next step. They can go pretty much anywhere on your system, that’s just where I chose.

With the gz files for Node versions in the correct folder, we extract the binary to a subfolder with tar -xzf ~/.local/share/node-v10.15.3-darwin-x64.tar.gz -C ~/.local/share/node101503 --strip-components=1. Obviously adjust the file/folder names for your scenario and repeat for however many versions you need.

Adding a shell alias to switch Node versions

So now we have two or more Node binaries installed on our system, we just need to be able to switch easily between them.

To do this we will add two alias commands to our shell. I’m on zsh so that means editing my .zshrc file.

An ‘alias’ here is just similar to a text snippet. We write something simple and human friendly in the Terminal, press enter and that runs something more geeky that the computer understands

I added these two new alias commands with other alias’s I have defined, my two aliases for this look like this:

alias ns="export PATH=$HOME/.fzf/bin:$HOME/.local/share/node101503/bin:$HOME/bin:$HOME/.local/bin:/usr/local/bin:$HOME/.local/share/nvim/spec/node_modules/.bin:~/go/bin:$PATH"
alias nr="export PATH=$HOME/.fzf/bin:$HOME/.local/share/node220900/bin:$HOME/bin:$HOME/.local/bin:/usr/local/bin:$HOME/.local/share/nvim/spec/node_modules/.bin:~/go/bin:$PATH"

However, I have other things in there, like a path to Go and other tools that my Neovim config needs. Yours may not need to be that long for each.

The way I understand it, is that you can append more paths to your $PATH by adding a colon and then the new extra path. So if you only needed to add this new Node path, your export may be as simple as:

alias ns="export PATH=$HOME/.local/share/node101503/bin:$PATH"
alias nr="export PATH=$HOME/.local/share/node220900/bin:$PATH"

Save the file, quit your Terminal or re-source the .zshrc and try your new command. After each, you can check which Node version with node -v and check where it is looking for that Node with which node.

What the path?

By adding more paths to your $PATH (yes, I know, keep up), your system can look in multiple places for the commands you are asking it to run. Think of it as the equivalent of someone asking “Where is the flat head screwdriver?”, and you can say, “It’s either in my toolbox, or I left it out on the shelf” and the system dutifully goes and checks both locations. I believe that paths near the beginning of the export take precedence over paths near the end too.

Summary

My own workplace rejected using NVM due to security concerns, so while such tools are certainly more convenient, it may not always be practical. This is a bit of work, but when done, you should now have a simple way to swap between Node versions when you need to. The aliases and Node versions are entirely up to you but the good news is you don’t need to use a further tool.