To use a Node module in your applications, it must first be installed where Node can find it. To install a Node module, use the npm install <
module_name>
command to download the Node module to your development environment and place it in the node_modules
folder, where the install
command is run. For example, the following command installs the express
module:
npm install express
The output of the npm install
command displays the HTTP requests to download the necessary files as well as the dependency hierarchy that was installed with the module. For example, Figure 3.3 shows part of the output from installing the express
module.
Notice that several .tgz files are downloaded, decompressed, and installed. Figure 3.3 also shows the dependency hierarchy: You can see that express
requires the methods
, cookie-signature
, range-parser
, debug
, buffer-crc32
, fresh
, cookie
, mkdirp
, commander
, send
, and connect
modules. All these modules were downloaded during the install. Notice that the version of each dependency module is listed.
Node.js has to be able to handle dependency conflicts. For example, the express
module requires cookie 0.1.0
, but another module may require cookie 0.0.9
. To handle this situation, a separate copy for the cookie module is placed in each module’s folder, under another node_modules
folder.
To see how modules are stored in a hierarchy, consider the following example of how express
looks on disk. Notice that the cookie
and send
modules are located under the express
module hierarchy and that because the send
module requires mime
, it is located under the send
hierarchy:
./
./node_modules
./node_modules/express
./node_modules/express/node_modules/cookie
./node_modules/express/node_modules/send
./node_modules/express/node_modules/send/node_modules/mime