Introduction to npm package manager

A quick guide to npm, this is a powerful package manager, and it is the key to Node.js's success. In January 2017, it was reported that more than 350,000 packages were listed in the npm registry, making it the largest single language code repository on the planet, and you can be sure of packages with (almost!) everything .

Introduction to npm

npmIt is the standard package manager for Node.js.

In January 2017, it was reported that more than 350,000 packages were listed in the npm registry, making it the largest single language code repository on the planet, and you can be sure of packages with (almost!) everything .

It started as a way to download and manage dependenciesNode.jsSoftware package, but since then it has become a tool used in the front endJavaScript.

There are many thingsnpmdo.

yarnIt is an alternative to npm. Make sure you also check it out.

installation

npmIt was installed when Node.js was installed. Go tohttps://nodejs.orgAnd install Node (if it is not already installed on the system).

How to use npm

npmManage the download of project dependencies.

Install all dependencies

If a project has onepackages.jsonFile, by running

npm install

It will install everything needed for the project innode_modulesThe folder, if it does not already exist, create it.

Install a single package

You can also install specific packages by running the following commands

npm install <package-name>

Generally, you will see more flags in this command:

  • --saveInstall and add entries topackage.jsonfileDependency(The default is npm 5)
  • --save-devInstall and add entries topackage.jsonfiledevDependencies

The main difference is that devDependencies are usually development tools, such as test libraries, anddependenciesHas been bundled with applications in production.

Update package

By running, the update becomes very easy

npm update

npm will check all packages for a newer version that satisfies your versioning constraints.

You can specify a single package to update as well:

npm update <package-name>

Versioning

In addition to plain downloads, npm also manages versioning, so you can specify any specific version of a package, or require a version higher or lower than what you need.

Many times you’ll find that a library is only compatible with a major release of another library.

Or a bug in the latest release of a lib, still unfixed, is causing an issue.

Specifying an explicit version of a library also helps to keep everyone on the same exact version of a package, so that the whole team runs the same version until the package.json file is updated.

In all those cases, versioning helps a lot, and npm follows the semantic versioning (semver) standard.

Running Tasks

The package.json file supports a format for specifying command line tasks that can be run by using

npm run <task-name>

For example:

{
  "scripts": {
    "start-dev": "node lib/server-development",
    "start": "node lib/server-production"
  },
}

It’s very common to use this feature to run Webpack:

{
  "scripts": {
    "watch": "webpack --watch --progress --colors --config webpack.conf.js",
    "dev": "webpack --progress --colors --config webpack.conf.js",
    "prod": "NODE_ENV=production webpack -p --config webpack.conf.js",
  },
}

So instead of typing those long commands, which are easy to forget or mistype, you can run

$ npm run watch
$ npm run dev
$ npm run prod  

Download my free Node.js Handbook


More node tutorials: