NodeJS & NPM

24-12-2020 / Edit on Github

Install NodeJS & NPM #

Install NodeJS and NPM.

Update to latest version?

# npm
npm cache clean -f # clear the cache first
sudo npm install -g npm
# 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 #

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

Install package #

👉 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
# or
npm i git+ssh://[email protected]/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

Update package #

# 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 package #

npm uninstall package

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 ./",
}