NodeJS & NPM draft

Last modified 1 year ago / Edit on Github
Danger icon
The last modifications of this post were around 1 year ago, some information may be outdated!
Danger icon
This is a draft, the content is not complete and of poor quality!

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.

Warning icon

Below commands are mostly for Linux/MacOS users.

Danger icon

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.

# FIRST INSTALL: the most recent lts release
nvm install --lts
# install a specific version
nvm install 12.13.0
# install latest version
nvm install node
# list all installed versions
nvm ls
# set default version of node
nvm alias default 12.13.0
# full list of available versions
# be careful, it's too long!!!
nvm ls-remote
# switch between versions
nvm use 12.13.0
# or (more quickly)
nvm use v15
# uninstall some version
nvm uninstall 12.13.0

Single version

👉 Install NodeJS and NPM: Windows & MacOS, Linux.

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

Shorthand CLI options

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

Install packages

👉 Official documentation.

npm install package_name # if it's in package.json, the indicated version will be installed
# otherwise, the newsest version will be installed
npm install --global package_name # global package
# install all package in package.json
npm install
# install + save to package.json
npm install --save package_name # save to package.json
npm install --save-dev package_name # save to package.json, in devDependencies
npm install --no-save package_name # don't save
# install with version
npm install [email protected]
# install a local package
npm install /path/to/package
// from github repository
npm i git+https://github.com/abc/xyz.git // https
npm i git+https://<github repo>#<new_commit_hash> // a specific commit
// or
npm i git+ssh://git@github.com/abc/xyz.git // ssh
# list all installed packages (current project only)
ls node_modules
# list all local (installed) packages
npm list # -g for globel # or use "ls"
npm list --depth=0 # without dependencies

# Check the current version of a (installed) package
npm list package_name # with "-g" for global

# Check the latest (not current) version of a package
npm view package_name version
# Set python2 by default when installing npm packages
npm config set python python2

Update packages

# which global packages need to be updated?
npm outdated -g --depth=0

# update all global packages
npm update -g
# update a package
npm update package_name # -g for global

Remove packages

npm uninstall package

Update package.json (npm version)

👉 Semantic Versioning 2.0.0 | Semantic Versioning
👉 npm-version | npm Docs

# Version: 1.2.3
# means: breaking.feature.fix

npm version patch # 1.0.0 -> 1.0.1 (fixes)
npm version minor # 1.0.1 -> 1.1.0 (new features )
npm version major # 1.1.0 -> 2.0.0 (completely new APIs)

Run scritps

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

Console.log things

Sometimes, we wanna log the results for debugging, but it appears [Object] (for example) all time.

import { inspect } from 'util';
console.log(inspect(
myObject,
{
showHidden: false,
depth: null,
colors: true
}
));

// One line
console.log(inspect(myObject, {showHidden: false, depth: null, colors: true}))
// Without module
const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null, colors: true}));

Troubleshooting

✴️ [Error: EACCES: permission denied, open '/Users/thi/.ngrok/...

# Error
sudo npm install ngrok -g

# Check the permission
ls -la /Users/thi/.ngrok

# Change the permission to "thi"
sudo chown -R $USER /Users/thi/.ngrok

💬 Comments

Support Thi Support Thi