npm

npm is the package manager for Node.js. It installs and manages packages: reusable pieces of code, written by other people, that a project can use.


What a Package Is#

Most programs need code that someone else has already written, for reading input, working with dates, drawing to the screen, and countless other tasks. Rather than write that code from scratch, a project uses a package: a bundle of ready-made code published for anyone to use.

A package a project relies on is called a dependency, because the project depends on it to work.

What npm Does#

Finding, downloading, and keeping track of packages by hand does not scale. A single project can rely on many packages, and each of those may rely on others.

npm handles this automatically. It fetches a requested package from the npm registry, a large online library of JavaScript packages, places it in the project, and records that the project depends on it. Updating or removing a package is managed the same way.

Installing a package into a project is a separate procedure: Install an npm Package.

npm Comes With Node.js#

npm is installed together with Node.js. Installing Node.js installs npm at the same time, so there is nothing extra to download. If Node.js is on the machine, npm is too.

⚠️ Mistakes

Thinking npm is separate from Node.js

npm is not a separate download. It is installed with Node.js. If the npm command is not found, the usual cause is that Node.js itself is not installed or not on the PATH, not a missing npm.

Confusing npm with the packages it installs

npm is the tool that installs packages. It is not itself the code a project uses. A package like a date library or an input reader is a separate thing that npm fetches and manages.

Thinking a package is part of the language

Packages are extra code added to a project, not built into JavaScript or Node.js. A feature that comes from a package is only available once that package has been installed into the project.

📚 Resources