NodeJS + npm

Anh-Thi Dinh

Install NodeJS & NPM

Install multiple versions

First, need to install nvm. Run the line of curl and then run/add-to-zsh the line of export.
⚠️
Below commands are mostly for Linux/MacOS users.
⚠️
For Mac M1: You may encouter error Target architecture arm64 is only supported on arm64 and x64 host when installing NodeJS version <= 14 with nvm. Just open Terminal using Rosetta (right click on Terminal.app > Get info > tick "Open using Rosetta") and then run the installation command again. 💡 Tip: You can create a separated Terminal Rosetta.app just in case you wanna install something using Rosetta.
1# FIRST INSTALL: the most recent lts release
2nvm install --lts
1# install a specific version
2nvm install 12.13.0
1# install latest version
2nvm install node
1# list all installed versions
2nvm ls
1# set default version of node
2nvm alias default 12.13.0
1# full list of available versions
2# be careful, it's too long!!!
3nvm ls-remote
1# switch between versions
2nvm use 12.13.0
3# or (more quickly)
4nvm use v15
1# uninstall some version
2nvm uninstall 12.13.0

Single version

1# UPDATE npm
2npm cache clean -f # clear the cache first
3sudo npm install -g npm
1# UPDATE node
2sudo npm install -g n
3sudo n stable
4# refresh the shell
5source ~/.zshrc # if using zsh
6source ~/.bashrc # is using bash
1# Check version
2npm -v
3node -v

Shorthand CLI options

  • i: install
  • D: -save-dev (devDependencies)
  • P: -save-prod (default), -save
  • g: -global
  • f: -force
  • ls: list

Install packages

1npm install package_name # if it's in package.json, the indicated version will be installed
2                         # otherwise, the newsest version will be installed
3npm install --global package_name # global package
1# install all package in package.json
2npm install
1# install + save to package.json
2npm install --save package_name # save to package.json
3npm install --save-dev package_name # save to package.json, in devDependencies
4npm install --no-save package_name # don't save
1# install with version
2npm install [email protected]
1# install a local package
2npm install /path/to/package
1# from github repository
2npm i git+https://github.com/abc/xyz.git // https
3npm i git+https://<github repo>#<new_commit_hash> // a specific commit
4# or
5npm i git+ssh://[email protected]/abc/xyz.git // ssh
1# list all installed packages (current project only)
2ls node_modules
1# list all local (installed) packages
2npm list # -g for globel # or use "ls"
3npm list --depth=0 # without dependencies
4
5# Check the current version of a (installed) package
6npm list package_name # with "-g" for global
7
8# Check the latest (not current) version of a package
9npm view package_name version
1# Set python2 by default when installing npm packages
2npm config set python python2

Meaning of ~ or ^ before package version

  • The node package version follows the structure major.minor.patch.
  • ~ → only update the patch to the latest, freezing major.minor.
  • ^ → only freezing major, update both minor.patch to the latest versions.

Update packages

1# which global packages need to be updated?
2npm outdated -g --depth=0
3
4# update all global packages
5npm update -g
1# update a package
2npm update package_name # -g for global
1# update/ugrade to a major version
2npm i packageName@<version>
 

Remove packages

1npm uninstall package

Update package.json (npm version)

1# Version: 1.2.3
2# means: breaking.feature.fix
3
4npm version patch # 1.0.0 -> 1.0.1 (fixes)
5npm version minor # 1.0.1 -> 1.1.0 (new features )
6npm version major # 1.1.0 -> 2.0.0 (completely new APIs)

Meaning of ~ or ^ before package version

  • The node package version follows the structure major.minor.patch.
  • ~ → only update the patch to the latest, freezing major.minor.
  • ^ → only freezing major, update both minor.patch to the latest versions.

Run scritps

1# Install first
2npm i --save npm-run-all
1// Run sequentially,
2// package.json
3"scripts": {
4	"build": "run-s prod:*", // "run-s" = "npm-run-all -s"
5	"prod:eleventy": "eleventy",
6	"prod:parcel": "parcel build ./ -o ./",
7}
1// Run parallely,
2// package.json
3"scripts": {
4	"start": "npm-run-all --parallel dev:*",
5	"dev:eleventy": "eleventy --serve",
6	"dev:parcel": "parcel watch ./ -o ./",
7}

Console.log things

Sometimes, we wanna log the results for debugging, but it appears [Object] (for example) all time.
1import { inspect } from 'util';
2console.log(inspect(
3  myObject,
4  {
5    showHidden: false,
6    depth: null,
7    colors: true
8  }
9));
10
11// One line
12console.log(inspect(myObject, {showHidden: false, depth: null, colors: true}))
1// Without module
2const util = require('util')
3console.log(util.inspect(myObject, {showHidden: false, depth: null, colors: true}));

Troubleshooting

✳️ [Error: EACCES: permission denied, open '/Users/thi/.ngrok/...
1# Error
2sudo npm install ngrok -g
3
4# Check the permission
5ls -la /Users/thi/.ngrok
6
7# Change the permission to "thi"
8sudo chown -R $USER /Users/thi/.ngrok