Skip to content

Instantly share code, notes, and snippets.

@Hansimov
Last active February 26, 2024 11:31
Show Gist options
  • Save Hansimov/64ad04670a27ce90cd02768b0a692563 to your computer and use it in GitHub Desktop.
Save Hansimov/64ad04670a27ce90cd02768b0a692563 to your computer and use it in GitHub Desktop.
Upgrade versions of npm and node.js to latest

Update Node.js via nvm

Install nvm

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Run bash to restart terminal to make the execution path recognizable.

Set http proxy for nvm:

nvm proxy        # Show current proxy
nvm proxy [url]  # Set proxy in format: `http://<server>:<port>

List available Node.js versions:

nvm ls available

which would output like:

|   CURRENT    |     LTS      |  OLD STABLE  | OLD UNSTABLE |
|--------------|--------------|--------------|--------------|
|    20.1.0    |   18.16.0    |   0.12.18    |   0.11.16    |
|    20.0.0    |   18.15.0    |   0.12.17    |   0.11.15    |
|    19.9.0    |   18.14.2    |   0.12.16    |   0.11.14    |
...
|    18.9.1    |   16.15.0    |    0.12.1    |    0.9.12    |
|    18.9.0    |   16.14.2    |    0.12.0    |    0.9.11    |
|    18.8.0    |   16.14.1    |   0.10.48    |    0.9.10    |

This is a partial list. For a complete list, visit https://nodejs.org/download/release

Choose a recent LTS version of Node.js to install:

nvm install 18.16.0
nvm use 18.16.0

Update npm

You may encountered an error when running:

npm install -g npm@latest
ERROR: npm v9.5.1 is known not to run on Node.js v18.16.0. You'll need to upgrade
to a newer Node.js version in order to use this version of npm. This version of
npm supports the following node versions: `^14.17.0 || ^16.13.0 || >=18.0.0`. You
can find the latest version at https://nodejs.org/.

This is caused by the Windows Paths issue. See the references below for more info.

Just remove all npm and nodejs related paths from user and system environment variables, and re-organize in the order below:

image

You need to put the npm install path of user (C:\Users\***\AppData\Roaming\npm) up to the nodejs install path (C:\sw\nodejs).

Use following commands to check your paths:

env
which nvm   # C:\Users\***\AppData\Roaming\nvm\nvm
which node  # C:\sw\nodejs
which npm   # C:\Users\***\AppData\Roaming\nvm\nvm

Then restart the shell (PowerShell may prevent you, so try cmder), and run the following again:

npm install -g npm@latest

Use following command to set http proxy for npm:

npm config set proxy http://<server>:<port>

And remove use following commands:

npm config rm proxy
npm config rm https-proxy

# To check configs
npm config list

Change registry for npm

npm config get registry

npm config set registry https://registry.npmmirror.com

npm config get registry

Relations of nvm, node and npm

nvm (Node Version Manager) is a tool that allows you to easily install and manage multiple versions of node (Node.js) on the same system. npm (Node Package Manager) is the default package manager for Node.js and is used to install and manage packages (libraries and tools) for use in Node.js projects.

The relationship between these three tools is as follows:

  • nvm is used to install and manage different versions of node.
  • Each version of node comes with a specific version of npm.
  • npm is used to install and manage packages for use in Node.js projects.

In summary, nvm is used to manage node versions, and each version of node comes with a specific version of npm, which is used to manage packages for Node.js projects.

References:

@Hansimov
Copy link
Author

Hansimov commented Jun 13, 2023

Quickly install npm module via proxy

# Run these 2 lines to set registry
npm config get registry
npm config set registry https://registry.npmjs.org

npm config set strict-ssl false
npm --proxy http://<server>:<port> install reveal.js

More details for proxy config in different envs:

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment