npm

Getting started with npm

Remarks#

npm is the default package manager for Node.js. It is written entirely in JavaScript, and allows to install and distribute mostly JavaScript modules on the registry. Packages are treated in the CommonJS format, and are recognizable by the presence of a package.json file.

Versions#

VersionRelease Date
v4.0.02016-10-21
v3.10.02016-06-17
v3.9.02016-05-06
v2.15.02016-03-11
v3.8.02016-02-26
v3.7.02016-01-29
v3.6.02016-01-21
v3.5.02015-11-20
v3.4.02015-11-06
v3.3.02015-08-14
v2.14.02015-08-14
v3.2.02015-07-25
v3.1.02015-07-03
v2.13.02015-07-03
v3.0.02015-06-26
v2.12.02015-06-19
v2.11.02015-05-22
v2.10.02015-05-08
v2.9.02015-04-24
v2.8.02015-04-10
v2.7.02015-02-27
v2.6.02015-02-13
v2.5.02015-01-30
v2.4.02015-01-23
v2.3.02015-01-16
v2.2.02015-01-09
v2.1.02014-09-26
v2.0.02014-09-13
v1.4.02014-02-13

Installation or Setup

Install

npm is bundled with Node.js, so if you install Node.js you’ll automatically have npm installed too. You can choose between a Current and a LTS version

Windows

For Microsoft Windows you can download a MSI installer from https://nodejs.org/en/download/.

OS X

For Apple OS X you can download a PKG installer from the same location https://nodejs.org/en/download/.

Linux

For Linux you can use your package manager to install Node.js and npm.

You can also compile Node.js from source and you’ll still get npm. There is also a script you can run which will install npm:

curl -L https://www.npmjs.com/install.sh | sh

Upgrade npm to the latest version

The recommended method of updating your npm installation is to simply have npm install itself:

npm install -g npm@latest

You can alternatively upgrade to the current LTS version rather than the latest version:

npm install -g npm@lts

You can also install any version of Node (and npm) with nvm. When installing globally with npm with an nvm installation, you do not need to use sudo (or Run as Administrator on Windows).

Install packages

Notice that packages can be installedThis command installs the newest available version of the named packages:

both locally or globally.

Local installation means that npm installs your package in the current working directory. Node modules go in ./node_modules, executables go in ./node_modules/.bin/. Usually you’ll want to install local modules for usage inside your program, as a dependency, and they will work only on where they’re installed.

npm install <package names> 

Shorthand:

npm i <package names>

npm can interact with a package.json file in the current directory in various useful ways, through the objects dependencies and devDependencies stored in package.json (installing multiple modules):

The npm install command with no parameters

npm install 

installs all packages named as object keys in the dependencies and devDependencies objects in package.json, using semantic versioning restrictions as indicated by the object values.

When developing new software:

Use option -S to append the <package names> and versions of npm modules you are installing that should always be included with your module. Appends to the list of dependencies tracked in the package.json file, after installing.

npm i <package names> -S

Use option -D to append the <package names> and versions of npm modules you are installing that are needed by other developers to further develop or test your module. Appends to the list of devDependencies tracked in the package.json file, after installing.

npm i <package names> -D

Where lodash and mocha are package names.

Installing Global Packages

Install a global package

Globally installed packages drops modules in {prefix}/lib/node_modules, and puts executable files in {prefix}/bin, where {prefix} is usually something like /usr/local. Installing a global module means that its binaries end up in your PATH environment variable. Usually you’ll want to install a global module if it’s a command line tool, or something that you want to use in your shell.

npm install --global package-name

Remove a global package

npm uninstall --global package-name

Note: the --global argument can be simplified to -g. So, for instance, the first command could have been npm install -g package-name, with the exact same outcome.

Note: in *nix systems, installing global packages may require super-user permissions. Failing to do so will fail with: EACCES. In that case, run:

sudo npm install --global package-name

Updating packages

In every applications life-cycle comes a day where it’s components needs to be updated. Everyone knows the pain of updating every single dependency one-by-one. Well here you just need to issue the command:

npm update (-g)

If the “-g” is there then npm will update the global packages.

Using npm to manage dependencies

So you want to deploy your app to multiple sites? and your project has too many dependencies to install them one-by-one? Npm has a solution just issue the following command:

npm init

In the project’s root folder then follow the instructions on screen (type in the desired value then press enter) and then if you want to save a dependency then add:

--save

after your

npm install

commands for example:

npm install mypackagename --save

And then that dependency will be saved then you don’t have to move the “node_modules” folder. In order to install all saved dependency issue:

npm install

and all saved dependencies will be installed.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow