Skip to content

Latest commit

 

History

History
121 lines (92 loc) · 4.41 KB

File metadata and controls

121 lines (92 loc) · 4.41 KB

Node-js

How to install,update Node with NPM via NVM

NVM needs to be installed on your host, follow the instructions to integrate into the matrix.dot.files environment

Note: For the latest installer version nvm-releases

The PROFILE='/dev/null' setting disables NVM from modifying your .bashrc in the matrix.

PROFILE='/dev/null'
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

or use the dynamic method identifying the latest release

(cd "$NVM_DIR" && git fetch --tags origin && git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`) &&
PROFILE='/dev/null' source "$NVM_DIR/nvm.sh"

Exit and Restart your shell.

Now you can list the latests versions of node, install the latest version of node nvm install node, and ensure you nvm use node it.

nvm ls-remote
nvm install node
nvm use node

Confirm that you shell is now using the latest (in this case v22.3.0)

nvm ls
->      v24.9.0
default -> node (-> v24.9.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v24.9.0) (default)
stable -> 24.9 (-> v24.9.0) (default)
lts/* -> lts/jod (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.20.8 (-> N/A)
lts/iron -> v20.19.5 (-> N/A)
lts/jod -> v22.20.0 (-> N/A)
...
node --version
v24.9.0
npm --version
11.6.0

Is there a NPM virtual manager?

Yes, and its preferred setup is NVM already enabled in the nodejs extension.

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

It also attempts to load bash_completions and print the current NVM version when present.

How do I run local bins in the path?

  • NPM is used for installing packages within the scope of a single project, while NPM global is used for installing packages system-wide, allowing you to access them from any project.
    • NPM Global When you install a package globally with npm (using npm install -g <package_name>), it adds the package to your system's global directory, which is typically located at /usr/local or where Node.js is installed on your system. This allows you to use that package in any project on your computer without needing to re-install it locally.
    • Under NVM Context When you install a package globally with NPM in an NVM environment, that package is installed into the global node_modules directory of that specific NVM environment. The global node_modules directory is determined by the path specified when you create or switch to a particular NVM environment.
  • NPX allows running commands directly from an npm package without having to install it first, providing a convenient way to use CLI tools and executables without cluttering your local or global installation.

Note: Dont confuse pipx with npx as they are not similar. Additionally, npx-alias still only provides current context executions unlike pipx which holds its own context.

Don't get confused on with your PATH

With a setup like lessc on Global @version 4.1.1

$ ls -la /Users/me/.nvm/versions/node/v15.0.1/bin/lessc
lrwxr-xr-x  1 me  staff  34 Feb 15 10:54 /Users/me/.nvm/versions/node/v15.0.1/bin/lessc -> ../lib/node_modules/less/bin/lessc

And $PROJECT with lessc @version 2.7.1

$ ls -la ./node_modules/less/bin/lessc
-rwxr-xr-x  1 smacgregor  staff  16275 Apr 22  2016 node_modules/less/bin/lessc

You can see how this works.

# Defaults to Global NPM/NVM scope.
$ lessc -v
lessc 4.1.1 (Less Compiler) [JavaScript]
## Works to load in $PROJECT lessc even without quotes, but fails outside of project
$ npx lessc -v
lessc 2.7.1 (Less Compiler) [JavaScript]
## Work to load in $PROJECT lessc and needs quotes, then looks at nvm/npm versioned global scope!
$ npm exec -c 'lessc -v'
lessc 2.7.1 (Less Compiler) [JavaScript]
## Work to load in $PROJECT lessc and needs quotes, then looks at true global scope!
$ npx -c 'lessc -v'
lessc 4.1.1 (Less Compiler) [JavaScript]

Note: (common syntax problems)

If you see version outputs like 7.0.1 or something not looking like lessc x.x.x (Less Compiler) .. thats NOT lessc, thats node reporting it's version.

--ALERT-- npm exec lessc -v
--ALERT-- 7.0.3