Node.js


npm v0.10–v7.2.0

v0.1
v0.2
v0.3
v0.4
v0.5
v0.6
v0.7
v0.8
v0.9
v0.10
v0.11
io.js v1.0
io.js v1.1
io.js v1.2
io.js v1.3
io.js v1.4
io.js v1.5
io.js v1.6
io.js v1.7
io.js v1.8
io.js v2.0
io.js v2.1
io.js v2.2
io.js v2.3
io.js v2.4
io.js v2.5
io.js v3.0
io.js v3.1
io.js v3.2
io.js v3.3
v4.0
v4.1
v4.2
v5.0
v5.1
v5.2
v5.3
v5.4
v5.5
v0.12
v4.3
v5.6
v5.7
v4.4
v5.8
v5.9
v5.10
v5.10.1
v5.11.0
v6.0.0
v5.11.1
v6.1.0
v6.2.0
v6.2.1
v6.2.2
v5.12.0
v6.3.0
v6.3.1
v6.4.0
v6.5.0
v6.6.0
v6.7.0
v6.8.0
v6.8.1
v6.9.0
v6.9.1
v7.0.0
v7.1.0
v7.2.0
v6.9.2
v7.3.0

Improvements requested:

  • This topic duplicates material from other topics, is deprecated, is obsolete, or is otherwise superfluous. –  Aurora0001 Nov 28 at 19:18
    Should this topic be moved to/merged with the npm tag: http://stackoverflow.com/documentation/npm/topics ? It seems that this section is more popular so perhaps the other section should be removed.

This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 83

    Introduction

    Packages are a term used by npm to denote tools that developers can use for their projects. These include everything from libraries and frameworks such as jQuery and AngularJS to task runners such as Gulp.js. The packages will come in a folder typically called node_modules, which will also contain a package.json file. This file will give information regarding the package including any dependencies, which are additional modules needed to use the package.

    Npm uses the command line to both install and manage packages, so users attempting to use npm should be familiar with basic commands on their operating system i.e.: traversing directories as well as being able to see the contents of directories.


    How to install packages

    To install one or more packages use the following. Note that this will install the package in the directory that the command line is currently in, thus it is important to check whether the appropriate directory has been chosen:

    npm install <package-name>
    # or
    npm i <package-name>...
    
    # e.g. to install lodash and express
    npm install lodash express
    

    If you already have a package.json file in your current working directory and dependencies are defined in it, then npm install will automatically resolve and install all dependencies listed in the file. You can also use the shorthand version of the npm install command which is: npm i

    If you want to install a specific version of a package use:

    npm install <name>@<version>
    
    # e.g. to install version 4.11.1 of the package lodash
    npm install [email protected]
    

    If you want to install a version which matches a specific version range use:

    npm install <name>@<version range>
    
    # e.g. to install a version which matches "version >= 4.10.1" and "version < 4.11.1"
    # of the package lodash
    npm install lodash@">=4.10.1 <4.11.1"
    

    If you want to install the latest version use:

    npm install <name>@latest
    

    The above commands will search for packages in the central npm repository at npmjs.com. If you are not looking to install from the npm registry, other options are supported, such as:

    # packages distributed as a tarball
    npm install <tarball file>
    npm install <tarball url>
    
    # packages available locally
    npm install <local path>
    
    # packages available as a git repository
    npm install <git remote url>
    
    # packages available on GitHub
    npm install <username>/<repository>
    
    # packages from a specific repository
    npm install --registry=http://myreg.mycompany.com <package name>
    
    # packages from a related group of packages 
    # See npm scope
    npm install @<scope>/<name>(@<version>)
    
    # Scoping is useful for separating private packages hosted on private registry from
    # public ones by setting registry for specific scope
    npm config set @mycompany:registry http://myreg.mycompany.com
    npm install @mycompany/<package name>
    

    Usually, modules will be installed locally in a folder named node_modules, which can be found in your current working directory. This is the directory require() will use to load modules in order to make them available to you.

    If you already created a package.json file, you can use the --save (shorthand -S) option or one of its variants to automatically add the installed package to your package.json as a dependency. If someone else installs your package, npm will automatically read dependencies from the package.json file and install the listed versions. Note that you can still add and manage your dependencies by editing the file later, so it's usually a good idea to keep track of dependencies, for example using:

    npm install --save <name> # Install dependencies 
    # or
    npm install -S <name> # shortcut version --save 
    # or
    npm i -S <name>
    

    Installing dependencies

    Some modules do not only provide a library for you to use, but they also provide one or more binaries which are intended to be used via the command line. Although you can still install those packages locally, it is often preferred to install them globally so the command-line tools can be enabled. In that case, npm will automatically link the binaries to appropriate paths (e.g. /usr/local/bin/<name>) so they can be used from the command line. To install a package globally, use:

    npm install --global <name>
    # or
    npm install -g <name>
    # or
    npm i -g <name>
    
    # e.g. to install the grunt command line tool
    npm install -g grunt-cli
    

    If you want to see a list of all the installed packages and their associated versions in the current workspace, use:

    npm list
    npm list <name>
    

    Adding an optional name argument can check the version of a specific package.


    Note: If you run into permission issues while trying to install an npm module globally, resist the temptation to issue a sudo npm install -g ... to overcome the issue. Granting third-party scripts to run on your system with elevated privileges is dangerous. The permission issue might mean that you have an issue with the way npm itself was installed. If you're interested in installing Node in sandboxed user environments, you might want to try using nvm.

    If you have build tools, or other development-only dependencies (e.g. Grunt), you might not want to have them bundled with the application you deploy. If that's the case, you'll want to have it as a development dependency, which is listed in the package.json under devDependencies. To install a package as a development-only dependency, use --save-dev (or -D).

    npm install --save-dev <name> // Install development dependencies which is not                      include in producation 
    # or
    npm install -D <name>
    

    You will see that the package is then added to the devDependencies of your package.json.

    To install dependencies of a downloaded/cloned node.js project, you can simply use

    npm install
    # or
    npm i
    

    npm will automatically read the dependencies from package.json and install them.

  • 27

    To uninstall one or more locally installed packages, use:

    npm uninstall <package name>
    

    The uninstall command for npm has five aliases that can also be used:

    npm remove <package name>
    npm rm <package name>
    npm r <package name>
    
    npm unlink <package name>
    npm un <package name>
    

    If you would like to remove the package from the package.json file as part of the uninstallation, use the --save flag (shorthand: -S):

    npm uninstall --save <package name>
    npm uninstall -S <package name>
    

    For a development dependency, use the --save-dev flag (shorthand: -D):

    npm uninstall --save-dev <package name>
    npm uninstall -D <package name>
    

    For an optional dependency, use the --save-optional flag (shorthand: -O):

    npm uninstall --save-optional <package name>
    npm uninstall -O <package name>
    

    For packages that are installed globally use the --global flag (shorthand: -g):

    npm uninstall -g <package name>
    
  • 21

    Node.js package configurations are contained in a file called package.json that you can find at the root of each project. You can setup a brand new configuration file by calling:

    npm init
    

    That will try to read the current working directory for Git repository information (if it exists) and environment variables to try and autocomplete some of the placeholder values for you. Otherwise, it will provide an input dialog for the basic options.

    If you'd like to create a package.json with default values use:

    npm init --yes
    # or
    npm init -y 
    

    If you're creating a package.json for a project that you are not going to be publishing as an npm package (i.e. solely for the purpose of rounding up your dependencies), you can convey this intent in your package.json file:

    1. Optionally set the private property to true to prevent accidental publishing.
    2. Optionally set the license property to "UNLICENSED" to deny others the right to use your package.

    To install a package and automatically save it to your package.json, use:

    npm install --save <package>
    

    The package and associated metadata (such as the package version) will appear in your dependencies. If you save if as a development dependency (using --save-dev), the package will instead appear in your devDependencies.

    With this bare-bones package.json, you will encounter warning messages when installing or upgrading packages, telling you that you are missing a description and the repository field. While it is safe to ignore these messages, you can get rid of them by opening the package.json in any text editor and adding the following lines to the JSON object:

    [...]
    "description": "No description",
    "repository": {
      "private": true
    },
    [...]
    
Please consider making a request to improve this example.

Syntax

Parameters

ParameterExample
accessnpm publish --access=public
binnpm bin -g
editnpm edit connect
helpnpm help init
initnpm init
installnpm install
linknpm link
prunenpm prune
publishnpm publish ./
restartnpm restart
startnpm start
stopnpm start
updatenpm update
versionnpm version

Remarks

Remarks

Still have a question about npm? Ask Question

Topic Outline