package.json and downloads them (and everything they need) into the node_modules folder. Run it once after cloning a project.Guide
How to download packages into your project, remove them, and what the node_modules folder really is.
When you clone or download a project, it comes with a package.json (the shopping
list) but not the actual package code — that would be far too big to share. Your first
job is to turn that list into real files. One command does it:
Download everything the project needs
$ npm install Ctrl+`)npm install and wait — you'll see a progress summary when it finishesWith no package name, npm install reads package.json and downloads every listed package (and everything they depend on) into a new node_modules folder. You can shorten it to 'npm i'.
The first run can take a minute and creates two things: the node_modules folder
(the downloaded code) and, if it wasn't there already, a package-lock.json file
(a precise record of what got installed — more on that later).
Want to use a new library? Give npm install a package name. npm downloads it
and writes it into your package.json so the project remembers it.
Add a package to the project
$ npm install dayjs
$ # short form:
$ npm i dayjs Replace 'dayjs' with any package name from npmjs.com. You can add several at once: npm install dayjs zod.
Two options change where a package goes. They trip up almost every beginner, so here's the plain version:
-D (or --save-dev) — saves the package under
devDependencies instead of dependencies. Use it for tools you only
need while building, not for the finished site to run (a test runner, a type checker).
-g (global) — installs a command-line tool machine-wide
instead of inside this one project. Use it sparingly; pinning tools per-project is usually
better (the next chapters explain why).
Dev tool vs global tool
$ npm install -D playwright # dev-only, saved to devDependencies
$ npm install -g http-server # global command, available everywhere Rule of thumb: prefer local installs (no -g). A project that lists its tools locally 'just works' for the next person who runs npm install.
Changed your mind? npm uninstall deletes the code and removes it from package.json.
Remove a package
$ npm uninstall dayjs Also written 'npm remove' or 'npm rm'. Add -g to remove a globally-installed tool.
node_modules is where npm puts all the downloaded package code. Open it once out of
curiosity and you'll find hundreds of folders — because each package you asked for
brings its own dependencies, and so on. It's normal for it to be the biggest folder in your
project by far.
Three rules keep you out of trouble:
.gitignore (this project ignores it).npm install rebuilds it from scratch — a reliable fix we'll lean on in the troubleshooting chapter.Why is it safe to delete node_modules? Because package.json + package-lock.json together describe exactly how to rebuild it. Those two small files are the source of truth; node_modules is just the downloaded result.
Up next: why packages are split into dependencies and devDependencies, and how to tell which is which.
FAQ
package.json and downloads them (and everything they need) into the node_modules folder. Run it once after cloning a project..gitignore. Anyone can recreate it from package.json + package-lock.json by running npm install.npm install <pkg> installs into the current project's node_modules. npm install -g <pkg> installs a command-line tool machine-wide. Prefer local installs so each project pins its own versions.npm uninstall <pkg> deletes it from node_modules and removes its line from package.json.