/ npm

NPM

npm is a package manager and is used to install and manage dependencies.

npm is central to the vibrant JavaScript(and specifically the Node.js) community, by making module and code reusability across projects very simple. Currently, npm has upwards of 500,000 available packages.

npm is useful not only for server-side projects. Most popular front-end libraries like Bootstrap and Font Awesome are available too.

Note:

  • npm comes out of the box with Node.js so it’s necessary to install Node.js first
  • npm has no full form because of its usage beyond Node.js specific projects. It was formerly called Node Package Manager.

npm Usage

npm is commonly used from the command line. The commands given below are arguably the most important ones to get you started:

npm init
Running this command in your project’s root directory initializes it for use with npm by creating a package.json file. You will be prompted for the project’s name, description, author’s name and more. This information is then used to populate the package.json file, which will also hold the information about the project’s dependencies and requirements. You can alter that information manually later.

npm install [name-of-package]
This installs a package and all its dependencies automatically, and saves it in the package.json file. If you are installing a development dependency, you may want to use the --save-dev or -D switch. npm will then save the package as a development dependency.

Packages are installed locally in the node_modules directory in your project’s root directory. Sometimes you may want to have a package available across different projects. This is done with the --global or -g switch. This is often useful for development tools and command line utilities.

npm install
Running npm install in a project’s root directory without a specific package name, installs all the dependencies required for that project. Those are calculated according to the project’s package.json file. This demonstrates the power of npm, where a single command can fetch tens or hundreds of dependencies automatically for you, and is useful when you git clone a repository, for example.

More Information:

  • Node.js website: nodejs
  • The official website of npm, you can read about npm as well as search for the different available packages: npmjs
  • Read more about npm: Wikipedia
  • A Beginner’s Guide to npm: sitepoint
  • If you want a comprehensive video series, check this out: youtube
  • And here’s the official series from npm: youtube

npm cheatsheet

Package management

npm i Alias for npm install
npm install Install everything in package.json
npm install lodash Install a package
npm install --save-dev lodash Install as devDependency
npm install --save-exact lodash Install with exact
Note: —save is the default as of npm@5. Previously, using npm install without --save doesn’t update package.json.

Install names

npm i lodash NPM package
npm i lodash@latest Specify tag latest
npm i lodash@3.0.0 Specify version 3.0.0
npm i lodash@">=1 <2.0" Specify version range
npm i @org/lodash Scoped NPM package
npm i user/repo GitHub
npm i user/repo#master GitHub
npm i github:user/repo GitHub
npm i gitlab:user/repo GitLab
npm i /path/to/repo Absolute path
npm i https://site.com/archive.tgz Tarball via HTTP
npm i ./archive.tgz Tarball

Updating

npm update Update production packages
npm update --dev Update dev packages
npm update -g Update global packages
npm update lodash Update a package

Extra details list

# 1. NPM Command Lines.
# Local mode is the default.
# Use --global or -g on any command to operate in global mode instead.

# As of version 0.3, it is recommended to run npm as root.
# This allows npm to change the user identifier to the `nobody` user
# prior to running any package build or test commands.


npm <cmd> -h                                            # quick help on <cmd>
npm -l                                                  # display full usage info
npm adduser                                             # add a registry user account
npm bin                                                 # display npm bin folder
npm bugs <pkgname>                                      # bugs for a package in a web browser maybe
npm build <package-folder>                              # build a package (should not be called directly)
npm cache [add|ls|clean] <tarball|folder>               # manipulates packages cache
npm config [set|get|delete|list|edit] <key> <value>     # manage the npm configuration files
npm dedupe [package names...]  --tag                    # reduce duplication
npm decrecate <name>[@<version>] <message>              # deprecate a version of a package (must be the package owner)
npm docs [<pkg-name> [<pkg-name> ...]]                  # docs for a package in a web browser maybe
npm edit <name>[@<version>]                             # edit an installed package (default EDITOR set to "vi")
npm explore <name>[@<version>] [ -- <cmd>]              # browse an installed package
npm faq                                                 # commonly asked questions
npm help-search <some search terms>                     # search npm help documentation (rarely necessary to call directly)
npm help <topic> <some search terms>                    # get help on npm
npm init                                                # interactively create a package.json file
npm install <folder|name@<tag|version>|tarball>         # install a package (optional flags: --save, --save-dev, --save-optional, --save-exact)
npm link <pkgname>                                      # symlink a package folder
npm ls <pkg>                                            # list installed packages (as well as their dependencies)
npm outdated [<name> [<name> ...]]                      # check for outdated packages
npm owner [ls|add|rm] <pkg-name> <user>                 # manage package owners
npm pack [<pkg> [<pkg> ...]]                            # create a tarball from a package
npm prefix                                              # display prefix
npm prune [<name> [<name ...]] --production             # remove extraneous packages
npm publish <tarball|folder> [--tag <tag>]              # publish a package
npm rebuild [<name > [<name> ...]]                      # rebuild a package
npm repo <pkgname>                                      # open package repository in the browser
npm restart <name>                                      # start a package
npm rm <name>                                           # remove a package
npm root                                                # display npm root
npm run [<pkg>] [command]                               # run arbitrary package scripts
npm search [search terms ...] [--long]                  # search for packages
npm shrinkwrap                                          # lock down dependency versions
npm star <pkgname> [<pkg>, ...]                         # mark your favorite packages
npm stars [username]                                    # view packages marked as favorites
npm start <name>                                        # start a package
npm stop <name>                                         # stop a package
npm submodule <pkg>                                     # add a package as a git submodule
npm tag <name>@<version>                                # tag a published version
npm test <name>                                         # test a package
npm unpublish <name>[@<version>]                        # remove a package from the registry
npm unstar <pkgname> [<pkg>, ...]                       # Unfavorite specified p
npm update [-g] [<name> [<name> ...]]                   # update a package
npm version [<newversion> | major | minor | patch]      # bump a package version
npm view <name>[@<version>] [<field<[.<subfield>] ...]  # view registry info
npm whoami                                              # display npm username
NPM
Share this

Subscribe to NL Slack