
npm
The npm is the package manager used by Node to distribute modules. The npm can be used to install, update, and manage modules. Package managers are popular in other languages such as Python. The npm automatically resolves and updates dependencies for a package and hence makes your life easy.
Installing packages
There are two ways to install npm packages: locally or globally. If you want to use the module's functionality only for a specific Node project, you can install it locally relative to the project, which is default behavior of npm install
. Alternatively, there are several modules that you can use as a command-line tool; in this case, you can install them globally:
npm install request
The install
directive with npm
will install a particular module—request
in this case. To confirm that npm install
worked correctly, check to see whether a node_modules
directory exists and verify that it contains a directory for the package(s) that you installed.
As you start adding modules to your project, it becomes difficult to manage the version/dependency of each module. The best way to manage locally installed packages is to create a package.json
file in your project.
A package.json
file can help you in the following ways:
- Defining versions of each module that you want to install. There are times when your project depends on a specific version of a module. In this case, your
package.json
helps you download and maintain the correct version dependency. - Serving as a documentation of all the modules that your project needs.
- Deploying and packaging your application without worrying about managing dependencies every time you deploy the code.
You can create package.json
by issuing the following command:
npm init
After answering basic questions about your project, a blank package.json
is created with content similar to the following:
{ "name": "chapter9", "version": "1.0.0", "description": "chapter9 sample project", "main": "app.js", "dependencies": { "request": "^2.65.0" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "Chapter9", "sample", "project" ], "author": "Ved Antani", "license": "MIT" }
You can manually edit this file in a text editor. An important part of this file is the dependencies
tag. To specify the packages that your project depends on, you need to list the packages you'd like to use in your package.json
file. There are two types of packages that you can list:
dependencies
: These packages are required by your application in productiondevDependencies
: These packages are needed only for development and testing (for example, using the Jasmine node package)
In the preceding example, you can see the following dependency:
"dependencies": { "request": "^2.65.0" },
This means that the project is dependent on the request
module.
Note
The version of the module is dependent on the semantic versioning rules—https://docs.npmjs.com/getting-started/semantic-versioning.
Once your package.json
file is ready, you can simply use the npm install
command to install all the modules for your projects automatically.
There is a cool trick that I love to use. While installing modules from the command line, we can add the --save
flag to add that module's dependency to the package.json
file automatically:
npm install async --save npm WARN package.json chapter9@1.0.0 No repository field. npm WARN package.json chapter9@1.0.0 No README data async@1.5.0 node_modules/async
In the preceding command, we installed the async
module with the normal npm
command with a --save
flag. There is a corresponding entry automatically created in package.json
:
"dependencies": { "async": "^1.5.0", "request": "^2.65.0" },