Modularizing and Managing JavaScript
Installing and Maintaining Node Modules with npm
Problem
You’re new to Node. You’ve installed it, and played around with the core Node modules installed with Node. But now, you need something more.
Solution
The glue that holds the Node universe together is npm, the Node package manager. To install a specific module, use the following on the command line:
npm
install
packagename
If you want to install the package globally, so it’s accessible from all locations in the computer, use the following:
npm
install
-
g
packagename
When to install locally or globally is dependent on whether you’re going to require()
the module, or if you need to run it from the command line. Typically you install require()
modules locally, and executables are installed globally, though you don’t have to follow this typical usage. If you do install a module globally, you might need administrative privileges:
sudo
npm
install
-
g
packagename
Discussion
The solution demonstrated the most common use of npm: installing a registered npm module locally or globally on your system. However, you can install modules that are located in GitHub, downloaded as a tar file, or located in a folder. If you type:
npm
install
--
help
you’ll get a list of allowable approaches for installing a module:
npm
install
npm
install
<
pkg
>
npm
install
<
pkg
>
@
<
tag
>
npm
install
<
pkg
>
@
<
version
>
npm
install
<
pkg
>
@
<
version
range
>
npm
install
<
folder
>
npm
install
<
tarball
file
>
npm
install
<
tarball
Get Installing, maintaining, and publishing Node modules with npm now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.