How can I update NodeJS and NPM to the next versions?

后端 未结 30 1656
北荒
北荒 2020-11-22 08:07

I just installed Node.js and npm (for additional modules).

How can I update Node.js and the modules which I\'m using to the latest versions

相关标签:
30条回答
  • 2020-11-22 08:50

    Use n module from npm in order to upgrade node . n is a node helper package that installs or updates a given node.js version.

    sudo npm cache clean -f
    sudo npm install -g n
    sudo n stable
    sudo ln -sf /usr/local/n/versions/node/<VERSION>/bin/node /usr/bin/nodejs
    

    NOTE that the default installation for nodejs is in the /usr/bin/nodejs and not /usr/bin/node

    To upgrade to latest version (and not current stable) version, you can use

    sudo n latest

    To undo:

    sudo apt-get install --reinstall nodejs-legacy     # fix /usr/bin/node
    sudo n rm 6.0.0     # replace number with version of Node that was installed
    sudo npm uninstall -g n
    

    If you get the following error bash: /usr/bin/node: No such file or directory then the path you have entered at

    sudo ln -sf /usr/local/n/versions/node/<VERSION>/bin/node /usr/bin/nodejs
    

    if wrong. so make sure to check if the update nodejs has been installed at the above path and the version you are entered is correct.

    I would advise strongly against doing this on a production instance. It can seriously mess stuff up with your global npm packages and your ability to install new one.

    0 讨论(0)
  • 2020-11-22 08:51

    Updating npm is easy:

    npm install npm@latest -g
    
    0 讨论(0)
  • 2020-11-22 08:51

    First check your NPM version

    npm -v
    

    1) Update NPM to current version:

    View curent NPM version:

    npm view npm version
    

    Update npm to current version:

    npm i -g npm
    


    2) List all available NPM versions and make a custom install/update/roll-back

    View all versions including "alpha", "beta" and "rc" (release candidate)

    npm view npm versions --json
    

    Reinstall NPM to a specific version chosen from the versions list - for example to 5.0.3

    npm i -g npm@5.0.3
    
    • Installing one version will automatically remove the one currently installed.

    • For Linux and iOS prepend commands with sudo

    0 讨论(0)
  • 2020-11-22 08:51

    First update npm,

    npm install -g npm@next

    Then update node to the next version,

    npm install -g node@next or npm install -g n@next or, to the latest,

    npm install -g node@latest or npm install -g node

    check after version installation,

    node --versionor node -v

    0 讨论(0)
  • 2020-11-22 08:51

    SIMPLY USE THIS

    npm i -g npm
    

    This is what i get promped on my console from npm when new update/bug-fix are released:

    0 讨论(0)
  • 2020-11-22 08:51

    Try the latest stable version of npm

    See what version of npm you're running:

    npm -v
    

    Upgrading on *nix (OSX, Linux, etc.)

    (You may need to prefix these commands with sudo, especially on Linux, or OS X if you installed Node using its default installer.)

    You can upgrade to the latest version of npm using:

    npm install -g npm@latest
    

    Or upgrade to the most recent release:

    npm install -g npm@next
    

    Upgrading on Windows


    By default, npm is installed alongside node in

    C:\Program Files (x86)\nodejs

    npm's globally installed packages (including, potentially, npm itself) are stored separately in a user-specific directory (which is currently

    C:\Users\<username>\AppData\Roaming\npm).

    Because the installer puts

    C:\Program Files (x86)\nodejs

    before

    C:\Users\<username>\AppData\Roaming\npm

    on your PATH, it will always use the version of npm installed with node instead of the version of npm you installed using npm -g install npm@<version>.

    To get around this, you can do one of the following:

    • Option 1: edit your Windows installation's PATH to put %appdata%\npm before %ProgramFiles%\nodejs. Remember that you'll need to restart cmd.exe (and potentially restart Windows) when you make changes to PATH or how npm is installed.

    • Option 2: remove both of

      • %ProgramFiles%\nodejs\npm
      • %ProgramFiles%\nodejs\npm.cmd
    • Option 3: Navigate to %ProgramFiles%\nodejs\node_modules\npm and copy the npmrcfile to another folder or the desktop. Then open cmd.exe and run the following commands:

    cd %ProgramFiles%\nodejsnpm install npm@latest

    If you installed npm with the node.js installer, after doing one of the previous steps, do the following.

    • Option 1 or 2

      • Go into %ProgramFiles%\nodejs\node_modules\npm and copy the file named npmrc in the new npm folder, which should be %appdata%\npm\node_modules\npm. This will tell the new npm where the global installed packages are.
    • Option 3

      • Copy the npmrc file back into %ProgramFiles%\nodejs\node_modules\npm

    A brief note on the built-in Windows configuration

    The Node installer installs, directly into the npm folder, a special piece of Windows-specific configuration that tells npm where to install global packages. When npm is used to install itself, it is supposed to copy this special builtin configuration into the new install. There was a bug in some versions of npm that kept this from working, so you may need to go in and fix that up by hand. Run the following command to see where npm will install global packages to verify it is correct.

    npm config get prefix -g
    

    If it isn't set to <X>:\Users\<user>\AppData\Roaming\npm, you can run the below command to correct it:

    npm config set prefix "${APPDATA}/npm" -g
    

    Incidentally, if you would prefer that packages not be installed to your roaming profile (because you have a quota on your shared network, or it makes logging in or out from a domain sluggish), you can put it in your local app data instead:

    npm config set prefix "${LOCALAPPDATA}/npm" -g
    

    ...as well as copying %APPDATA%\npm to %LOCALAPPDATA%\npm (and updating your %PATH%, of course).

    Everyone who works on npm knows that this process is complicated and fraught, and we're working on making it simpler. Stay tuned.

    Source: https://docs.npmjs.com/troubleshooting/try-the-latest-stable-version-of-npm

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