How can I change the version of npm using nvm?

后端 未结 14 2150
温柔的废话
温柔的废话 2020-11-28 17:27

I\'ve been using NVM to install the latest versions of nodeJS for my node work. It works totally fine for installing separate versions and switching between them. It also in

相关标签:
14条回答
  • 2020-11-28 18:04

    nvm now has a command to update npm. It's nvm install-latest-npm or npm install --latest-npm.

    0 讨论(0)
  • 2020-11-28 18:07

    EDIT: several years since this question was first answered, as noted in a newer answer, there is now a command for this:

    nvm now has a command to update npm. It's nvm install-latest-npm or nvm install --latest-npm.

    nvm install-latest-npm: Attempt to upgrade to the latest working npm on the current node version

    nvm install --latest-npm: After installing, attempt to upgrade to the latest working npm on the given node version

    Below are previous revisions of the correct answer to this question.

    Over three years after this question was first asked, it seems like the answer is much simpler now. Just update the version that nvm installed, which lives in ~/.nvm/versions/node/[your-version]/lib/node_modules/npm.

    I just installed node 4.2.2, which comes with npm 2.14.7, but I want to use npm 3. So I did:

    cd ~/.nvm/versions/node/v4.2.2/lib
    npm install npm
    

    Easy!

    And yes, this should work for any module, not just npm, that you want to be "global" for a specific version of node.


    EDIT 1: In the newest version, npm -g is smart and installs modules into the path above instead of the system global path.


    Thanks @philraj for pointing this out in a comment.

    0 讨论(0)
  • 2020-11-28 18:09

    In windows, run your terminal as admin (in case there are permission issues as I had). Then use a specific node version (say 7.8.0) by

    nvm use 7.8.0
    

    then update your npm to desired specific version by

    npm install -g npm@5.0.3
    
    0 讨论(0)
  • 2020-11-28 18:13

    Changing npm versions on linux based OSs isn't a straight forward one command process yet. I have done following to switch back to older version of npm. This should work to get any version of npm working. First install the version of npm you want to use:

    sudo npm install -g npm@X.X.X
    

    Remove the sym link in /usr/local/bin/

    sudo rm /usr/local/bin/npm
    

    Recreate the sym link using the desired version of npm you have installed

    sudo ln -s /usr/bin/npm@X.X.X /usr/local/bin/npm
    
    0 讨论(0)
  • 2020-11-28 18:14

    I had same issue after installing nvm-windows on top of existing Node installation. Solution was just to follow the instructions:

    You should also delete the existing npm install location (e.g. "C:\Users\AppData\Roaming\npm") so that the nvm install location will be correctly used instead.

    Installation & Upgrades

    0 讨论(0)
  • 2020-11-28 18:15

    By looking at www.npmjs.com/install.sh I found there is a way to install a specific version by setting an environment-variable

    export npm_install="2.14.14"

    Then run the download-script as described at npmjs.com:

    curl -L https://www.npmjs.com/install.sh | sh

    If you omit setting the npm_install variable, then it will install the the version they have marked as latest

    0 讨论(0)
提交回复
热议问题