Node.js - npm notes

npm uses semantic versioning (aka semver)

Release type Example Description
Patch releases 1.0.0 -> 1.0.1 non-breaking changes. no new features. just minor bug fix.
Minor releases 1.0.1 -> 1.1.0 add a new features, which doesn’t break anything. just adds functionality.
Major releases 1.1.0 -> 2.0.0 breaking changes.

sample semver

Code status Stage Rule Example version
First release New product Start with 1.0.0 1.0.0
Backward compatible bug fixes Patch release Increment the third digit 1.0.1
Backward compatible new features Minor release Increment the middle digit and reset last digit to zero 1.1.0
Changes that break backward compatibility Major release Increment the first digit and reset middle and last digits to zero 2.0.0

semver tilde(~) and caret(^)

Symbol Dependency Versions Description
tilde(~) ~1.2.3 1.2.* can update patch version
caret(^) ^1.2.3 1.*.* can update minor and patch versions

ls

# list locally installed npm packages
npm ls --depth=0

# list globally installed npm packages
npm ls -g --depth=0

init

# create a new react-based project using create-react-app
npm init react-app ./my-react-app

# create a new esm-compatible package using create-esm
mkdir my-esm-lib && cd my-esm-lib
npm init esm --yes

# create a package.json file without asking any questions
mkdir my-npm-pkg && cd my-npm-pkg
git init
npm init -y

install

# install npm package globally
npm i -g webpack

# install all local npm packages (in package.json)
npm i

# install as a dependency
npm i lodash

# install as dev dependency
npm i -D webpack

uninstall

# uninstall global package
npm rm -g jshint

# uninstall local package
npm rm lodash

# uninstall local package and remove from package.json
npm rm --save lodash

update

# update global npm packages
npm up -g

# update local npm packages (in package.json)
npm up

prune

# remove extraneous packages
npm prune

some npm packages might not be updated to the latest

When you run npm update, some npm packages might not be udpated to the latest because it is a major release that can introduce breaking changes.

show outdated npm packages

# show outdated global npm packages
npm outdated -g

# show outdated local npm packages (in package.json)
npm outdated

npm outdated

update npm all packages to the latest versions

# must install npm-check-updates globally
npm i -g npm-check-updates

# list global npm packages that can be updated
ncu -g

# update all version hints (in package.json)
# and then update local npm packages
ncu -u
npm update

ncu

updating npm package version as publisher of an npm package

# increment patch part of our npm package version number
npm version patch
npm publish

# increment minor part of our npm package version number
npm version minor
npm publish

# increment major part of our npm package version number
npm version major
npm publish
Source

© 2020 | Paul Kim