101

For example I want to install either 7.9 or 7.10 but want to avoid Node 8 due to the webpack node-sass breaking bug.

When I run brew search node this is what I see:

❯ brew search node leafnode llnode node ✔ node-build > node@0.10 node@0.12 node@4 node@6 nodebrew nodeenv nodenv caskroom/cask/node-profiler

If you meant "node" specifically: It was migrated from caskroom/cask to homebrew/core. You can access it again by running: brew tap homebrew/core

There is node which is checked (my current version is v7.4.0 then node@0.10, node@0.12, node@4 and node@6?

The reason I can't just fully upgrade to 8 is node-sass won't work in webpack.


Just installed NVM and got this crazy error log:

=> nvm source string already in /Users/leongaban/.zshrc => Appending bash_completion source string to /Users/leongaban/.zshrc npm ERR! missing: is-path-cwd@^1.0.0, required by del@3.0.0 npm ERR! missing: is-path-in-cwd@^1.0.0, required by del@3.0.0 npm ERR! missing: p-map@^1.1.1, required by del@3.0.0 npm ERR! missing: pify@^3.0.0, required by del@3.0.0 npm ERR! missing: rimraf@^2.2.8, required by del@3.0.0 npm ERR! missing: bluebird@^3.1.1, required by gulp-html-replace@1.6.2 npm ERR! missing: clone@^1.0.2, required by gulp-html-replace@1.6.2

...

=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:

If I'm reading this right, does this mean I can't use npm to globally install packages anymore and have to use nvm?

Update

I added the export lines to my .zshrc bash (I don't use bash_profile)

❯ nvm --version
0.33.2
shilovk
  • 11,718
  • 17
  • 75
  • 74
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529

4 Answers4

163

There are two ways in the decision for using different version of Node. Second way is more convenient and practical by my opinion (also may be faster).


First way:

Install other Node version(for example 14) with using:

brew install node@14
brew unlink node
brew link node@14
node -v

PS You may use brew link with flag --overwrite, for example:

brew link --overwrite node@14

PS2 Why unlink and then link again?

Documentation:

Remove symlinks for formula from Homebrew's prefix. This can be useful for temporarily disabling a formula:

brew unlink formula && commands && brew link formula

In other words:

if you have both node and node@14 installed, where node is other version(..,15 or 16), so, for set active version 14:

you must unlink node and then link to new installed version 14
brew unlink node brew link node@14

Second way:

Install Node Version Manager(nvm) and select Node version:

nvm - github nvm - home brew

brew install nvm

mkdir ~/.nvm

export NVM_DIR="$HOME/.nvm"
    [ -s "$(brew --prefix)/opt/nvm/nvm.sh" ] && . "$(brew --prefix)/opt/nvm/nvm.sh" # This loads nvm
    [ -s "$(brew --prefix)/opt/nvm/etc/bash_completion.d/nvm" ] && . "$(brew --prefix)/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion

nvm install 14

nvm use 14

nvm list
shilovk
  • 11,718
  • 17
  • 75
  • 74
  • 1
    FYSA - you need to use `--overwrite` flag for `brew link` if you've previously linked node via brew... see @sangamangre suggest - `brew link --overwrite node@14` – SliverNinja - MSFT Nov 01 '21 at 20:24
  • @SliverNinja-MSFT In my case it works without this flag – shilovk Nov 02 '21 at 09:11
  • can you explain each command? why do you `unlink` and then `link` again? – vsync Dec 31 '21 at 13:22
  • @vsync I add why in PS2 – shilovk Dec 31 '21 at 23:33
  • Thank you! That worked, my nvim installation which was installed with node@18 was not working with copilot which requred <=17. This did the trick – Flov Aug 12 '22 at 20:23
  • this works but it is very slow to install node. I strongly recommend deleting all of node from brew and installing it with nvm – Fed Nov 03 '22 at 15:14
  • @FedericoCapaldo Ok, I add text that nvm may be faster – shilovk Nov 03 '22 at 15:27
  • I installed node using the exact code, but after installation getting- zsh: command not found: node – Pratikshit Singh Dec 03 '22 at 01:26
  • @PratikshitSingh try add to file `vi ~/.zshrc` this 3 lines: 1) `export PATH="/usr/local/opt/node@14/bin:$PATH"` 2) `export LDFLAGS="-L/usr/local/opt/node@14/lib"` 3) `export CPPFLAGS="-I/usr/local/opt/node@14/include"` – shilovk Dec 03 '22 at 08:13
35

If versions on homebrew/code are defined right, you must also be able to brew install node@0.12 for example.

You can also install multiple versions and select which one you want to use with the brew switch command.

--

Anyway, I'd recommend using nvm, which can be installed through Homebrew. Although, the version on brew is buggy and they don't plan fixing it.

balintant
  • 2,774
  • 1
  • 19
  • 33
  • Ok thanks, so if I go with nvm how do I decouple the version of node `7.4` on my machine which is currently installed via brew? – Leon Gaban Jun 28 '17 at 13:32
  • Would you mind a look at the new error messages I see right after installing nvm? – Leon Gaban Jun 28 '17 at 13:45
  • 4
    Ok so I installed `nvm` added those `export` lines to my .zshrc file, then did `nvm install v7.10` and now: `❯ node -v v7.10.0` `❯ npm -v 4.2.0` `❯ nvm --version 0.33.2` – Leon Gaban Jun 28 '17 at 13:57
  • @LeonGaban sounds good, any other questions? Your packages are now separated for different versions of node. :) – balintant Jun 28 '17 at 15:58
  • 19
    I found this only works for the major version: `brew install node@10` works, but `brew install node@10.15` does not. – JJ Zabkar May 09 '19 at 17:49
  • 3
    Yes, like @JJZabkar said there is no possible to choose specific version even version 11 is missing and now there is only 12. – Peter S. Nov 05 '19 at 10:23
23

To install the latest version of node and unlink the previously installed

brew install node@14
brew unlink node
brew link --overwrite node@14
# Facing an issue with the brew link run below command and try again
# sudo chown -R $(whoami) $(brew --prefix)/*  
echo 'export PATH="/usr/local/opt/node@14/bin:$PATH"' >> ~/.bash_profile
node -v
SangamAngre
  • 809
  • 8
  • 25
-2
brew install --build-from-source node@14

just use --build-from-source flag

MickJagger
  • 27
  • 4
  • 2
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Feb 22 '22 at 18:02