Package is a term used by npm to denote tools that developers can use for their projects. This includes 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 contains information regarding all the packages including any dependencies, which are additional modules needed to use a particular 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.
Note that in order to install packages, you must have NPM installed.
The recommended way to install NPM is to use one of the installers from the Node.js download page. You can check to see if you already have node.js installed by running either the npm -v or the npm version command.
After installing NPM via the Node.js installer, be sure to check for updates. This is because NPM gets updated more
frequently than the Node.js installer. To check for updates run the following command:
How to install packages
To install one or more packages use the following:
Note: 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
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:
If you want to install a version which matches a specific version range use:
If you want to install the latest version use:
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:
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:
In order to install packages and save them only if they are needed for development, not for running them, not if they are needed for the application to run, follow the following command:
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/
If you want to see a list of all the installed packages and their associated versions in the current workspace, use:
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).
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 will automatically read the dependencies from package.json and install them.
NPM Behind A Proxy Server
If your internet access is through a proxy server, you might need to modify npm install commands that access remote repositories. npm uses a configuration file which can be updated via command line:
You can locate your proxy settings from your browser's settings panel. Once you have obtained the proxy settings (server URL, port, username and password); you need to configure your npm configurations as follows.
username, password, port fields are optional. Once you have set these, your npm install, npm i -g etc. would work properly.