Best way to install and use nvm on Mac

Best way to install and use nvm on Mac

Image for post

As a developer, switch version of Node always for capable client?s demand or environment without review code again.

Image for post

and nvm is one way of switch versions of node.

Please NEVER install it in this way (brew install).

If you did, I will show you how to clear and install it in the correct way step by step.

on Mac, from the beginning I launch terminal:

$ brew install nvm

then write an alias to terminal profile as

$ echo “source $(brew –prefix nvm)/nvm.sh” >> .bash_profile

my terminal is iTerm, zsh and on-my-zsh not bash so it is

$ echo “source $(brew –prefix nvm)/nvm.sh” >> .zhsrc

activate configuration :

$ ~/.bash_profile

to zsh:

$ . ~/.zshrc

then choose any version of node, like v6.5.0, just type

$ nvm install v6.5.0

then use this version of node

$ nvm use v6.5.0

it shows something wrong as below:

nvm is not compatible with the npm config “prefix” option: currently set to “/Users/fabian/.nvm/versions/node/v6.5.0” Run `nvm use – delete-prefix v6.5.0` to unset it.

After studying some feedback on GitHub. there is a correct way to install nvm.

First of all, remove node files controlled by nvm.

$ npm ls -g ? depth=0#check all module installed.$ sudo rm -rf /usr/local/lib/node_modules#delete node_modules folders$ sudo rm /usr/local/bin/node#delete node$ cd /usr/local/bin && ls -l | grep “../lib/node_modules/” | awk ?{print $9}?| xargs rm#delete global node module alias

restart terminal, then install nvm by curl

Best Way to install NVM is HERE ?

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash

The reason to happen the message seems brew doesn?t write the correct alias to terminal profile, it will happen something wrong if install nvm via brew on Mac.

Final Step:

You?ll find something added into .bash_profile, if you use zsh, add below line to ~/.zshrc

export NVM_DIR=”$HOME/.nvm”[ -s “$NVM_DIR/nvm.sh” ] && . “$NVM_DIR/nvm.sh” # This loads nvm

DONE!! Terminal Happy!

you may want to check the official document of NVM, here is the link:https://github.com/nvm-sh/nvm

7

No Responses

Write a response