NPM Cheat Sheat

NPM is the default package manage system for Node.js

Check version

> npm -v
> node -v

Package.json

The init command creates a file called package.json after a few questions. This file contains all the information known about the project.

Initialize a new project

> npm init

Initialize a new project with default configuration

> npm init --yes

Packages

NPM allows us to install, update and uninstall packages in a project. NPM also works as a repository for open-source packages created by the community.

Install a package

> npm install <package>
> npm i <package>

The installed package is listed automatically in the package.json file, under the dependencies list. If you are on a older version (<5) you have to manually specify it.

> npm i <package> --save

Package dependencies

There are multiple types of dependencies, they are listed on the package.json file.

  • dependencies: these are the packages required by your application in production

  • devDependencies: packages that are only needed during development and testing

  • peerDependencies: these packages are only needed when publishing the package. when you include peerDependencies you define a specific version to be installed with your package. If this dependency exist it does nothing, if it does't exist or it is a different version it will show a warning.

  • optionalDependencies: these are optional dependencies, if they fail to install the installation process will continue.

  • bundleDependencies: is an array of packages that will be bundled with your package. This is useful when using a 3rd party package that is not available in NPM.

Install dependencies of a package.json

> npm i

Semantic Versioning

Semantic versioning (or SemVer for short) is a convention or standard when defining software version, depending on the nature of change that is being introduced.

  • Major: introduces a drastic change in the software. Usually not compatible with previous versions.

  • Minor: changes that add a new feature or modify an existing one, but it is still compatible with previous versions.

  • Patch: changes that fixes a bug and are backward compatible.

SemVer Structure

Major.Minor.Patch

Tilde Ranges (~)

Allow patch-level changes if a minor version is specified on the comparator. Allow minor-level changes if not.

~1.2.3: >=1.2.3 < 1.3.0

Carret Ranges (^)

Allow changes that don't modify major versions.

^1.2.3: >= 1.2.3 < 2.0.0

Static Range

Only allows to install a certain version.

1.2.3: == 1.2.3

Working with packages

Install / Uninstall a package

Install a specific verrsion of a package

> npm i <package>@<version>

Install a development package

> npm i <package>@<version>

Uninstall a package

> npm uninstall <package>
> npm un <package>

List

NPM list shows all the installed packages and their dependencies version in a tree structure.

Show installed packages and dependencies

> npm list

Show only installed packages

> npm list --depth=0

View

NPM view shows information about a certain package.

View package

> npm view <package>

View package dependencies

> npm view <package> dependencies

View all versions of a package

> npm view <package> versions

Outdated

This command will check the registry to see if any installed packages are currently outdated.

> npm outdated

Update

This command will update all the pacakes listed to the latest version, respecting SemVer.

> npm update

Update with npm-check-update (ncu)

npm-check-updates upgrades your package.json dependencies to the latest version, ignoring SemVer.

Install npm-check-update globaly

> npm i -g npm-check-updates

Update package.json

> npm-check-updates -u
> ncu -u

Update packages

> npm i

Publish a package

To publish a package you will need a NPM accout (you can register here), you can also register via terminal using the NPM CLI.

Register using NPM

> npm adduser

Login using NPM

> npm login

publish a package

> npm publish

Update a published package

To update a published pacakge you will need to have a new software version, you can change the version of the software manually, or change it via NPM CLI.

New major version

> npm version major

New minor version

> npm version minor

New patch version

> npm version patch

Finaly you can update your package

> npm publish