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:
npminstallpackagename
If you want to install the package globally, so it’s accessible from all locations in the computer, use the following:
npminstall-gpackagename
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:
sudonpminstall-gpackagename
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:
npminstall--help
you’ll get a list of allowable approaches for installing a module:
npminstallnpminstall<pkg>npminstall<pkg>@<tag>npminstall<pkg>@<version>npminstall<pkg>@<versionrange>npminstall<folder>npminstall<tarballfile>npminstall<tarball
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access