DEV Community

Clark
Clark

Posted on

How to install npm through NVM(Node version manager)?

The original post published on my blog: https://ms314006.github.io/how-to-install-npm-through-NVM-node-version-manager/

Hello guys, I am Clark! In this post I am going to share about install npm. In the official document of npm, you can install npm through two way, and npm is node package manager, so the two way are all related to node.

The first way is node installer which I always did when I want to install npm before, because this way is easily, you just need to go to the website of node, download the node installer and install it(Whatever you choose the LTS or Current version), you will got npm when you finished.

Alt Text

The second way is through NVM, also is main way I want to share in this post. But I never do it, so if any mistake I did, please comment below let me know, thanks!

What is the NVM

The NVM meaning is node version manager, according to the official document, something we can know:

  • This way is more recommend than node installer.
  • You can install and switch between multiple versions of node and npm in your device.

So if we choose the NVM, we can test if our application work well on different version of node. It sounds nice! Let's install NVM!

Window

If your device's OS is window, you can download the latest NVM installer from releases. Install NVM on the window is more easily than on the macOS I think.

If you finished download and install, you could open command line and type nvm, that would look like this if you success install NVM:

Alt Text

macOS

If your device's OS is macOS, you can execute a command below through terminal(According to here):



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


Enter fullscreen mode Exit fullscreen mode

If you finished the step and you already have installed npm before then NVM would check all modules you installed and ask if you want to uninstall they from global:

Alt Text

Because if you installed the NVM, your original node and npm will be overwritten by NVM. But this step we will handle later, because we have something not yet finished.

My terminal is using zsh(The latest macOS Catalina default terminal is use zsh), so I need a a new terminal window and add the following to file ./zshrc, if your terminal is not zsh, please add in file ./bash_profile:



vim ~/.zshrc


Enter fullscreen mode Exit fullscreen mode

And type i to start editing it, and paste the following:



export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion


Enter fullscreen mode Exit fullscreen mode

After you paste the command above, you can click button esc on keyboard, continue type :wq and enter it:

Alt Text

If you finished above all thing, please you close and reopen a new terminal then type nvm check if you successful installed:

Alt Text

Next if you want to use current versions of node and npm through your NVM, you can run following. The use also is command which you want to switch the version of node:



nvm use system


Enter fullscreen mode Exit fullscreen mode

The NVM will apply versions of node and npm to the NVM, we can continue run command below check:



nvm ls


Enter fullscreen mode Exit fullscreen mode

Alt Text

Anything will like you use node or npm before install NVM.

How to install other versions of node and npm

If you want to install other versions of node and npm, the first you can run following see all versions of node you can install:



nvm ls-remote


Enter fullscreen mode Exit fullscreen mode

Alt Text

Next if I want to install latest LTS version of node v12.18.3, I can run following:



nvm install <version>


Enter fullscreen mode Exit fullscreen mode

Then we can run the following command to check the version of node currently use by NVM:



nvm current


Enter fullscreen mode Exit fullscreen mode

So if I want to install the v12.18.3 and check if it is successful switch to versions I installed:

Alt Text

So good! You also can uninstall version of node you don't want to use:



nvm uninstall <version>


Enter fullscreen mode Exit fullscreen mode

Last, you should be know, the version of node would not influences each other, each version of node will be independent! So just take easy to using any version of node to develop your applications!

Final words

I hope this post can be helpful for you, and if you have any opinions or questions, please comment below, I would very appreciate every thing! :)

Top comments (4)

Collapse
 
ruanltbg profile image
Ruan Carlos

On M1 chip I get this:

$ nvm use system
System version of node not found.

$ nvm use v15.14.0
Now using node v15.14.0 (npm v)

$ npm
node:internal/modules/cjs/loader:927
  throw err;
  ^

Error: Cannot find module '/usr/local/bin/npm'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:924:15)
    at Function.Module._load (node:internal/modules/cjs/loader:769:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
Enter fullscreen mode Exit fullscreen mode

Any idea?

Collapse
 
rainbowwzrd profile image
Rainbow Wizard

top

Collapse
 
kattak2k profile image
Ravi Katta

Looking exactly for this, Nicely written. Thank you

Collapse
 
tngeene profile image
Ted Ngeene

awesome. thanks!