Installing Packages

Setting up a project: npm install

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

Terminal
$ npm install
In VS Code
  1. Open the project folder in VS Code
  2. Open the terminal (Ctrl+`)
  3. Run npm install and wait — you'll see a progress summary when it finishes

With 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).

Adding a new package

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

Terminal
$ npm install dayjs
$ # short form:
$ npm i dayjs
In VS Code
  1. Run the command in the integrated terminal
  2. Watch package.json — a new line appears under "dependencies"
  3. The package is now in node_modules, ready to import in your code

Replace 'dayjs' with any package name from npmjs.com. You can add several at once: npm install dayjs zod.

The two important flags: -D and -g

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

Terminal
$ npm install -D playwright    # dev-only, saved to devDependencies
$ npm install -g http-server   # global command, available everywhere
In VS Code
  1. These are terminal commands
  2. After a global install, the new command works in any folder; a -D install only affects this project

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.

Removing a package

Changed your mind? npm uninstall deletes the code and removes it from package.json.

Remove a package

Terminal
$ npm uninstall dayjs
In VS Code
  1. Run it in the terminal
  2. The package's line disappears from package.json and its files leave node_modules

Also written 'npm remove' or 'npm rm'. Add -g to remove a globally-installed tool.

What is the node_modules folder?

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:

  • Never edit anything inside it. It's generated code; your changes get wiped on the next install.
  • Never commit it to Git. It's huge and fully rebuildable, so it belongs in .gitignore (this project ignores it).
  • If it breaks, delete it and reinstall. 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

Frequently Asked Questions

What does "npm install" do with no package name?
It reads every package listed in package.json and downloads them (and everything they need) into the node_modules folder. Run it once after cloning a project.
Should I commit the node_modules folder to Git?
No. It's huge and fully rebuildable, so it's listed in .gitignore. Anyone can recreate it from package.json + package-lock.json by running npm install.
What is the difference between a local and a global 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.
How do I remove a package?
npm uninstall <pkg> deletes it from node_modules and removes its line from package.json.