🚀Backend Internals #5: Stop Installing Everything Globally—Understand Local vs Global npm Packages
One of the most confusing topics for beginners in Node.js isn't Express, APIs, or asynchronous programming—it's understanding where npm packages should be installed. When I started learning Node.js, I thought there were only two commands: npm install package-name and npm install -g package-name I knew they both installed packages, but I had no idea when to use which one . Eventually, I realized they solve two completely different problems. If you're learning Node.js, this article will save you from one of the most common beginner mistakes. First, What Does npm Actually Do? npm (Node Package Manager) is the package manager that comes with Node.js. It helps you: Install libraries Manage project dependencies Update packages Share your own packages Run project scripts Whenever you install a package, npm has to decide where to install it. That's where local and global installations come in. Local Installation (The Default) When you run: npm install express npm installs Express inside your current project . Your folder now looks something like this: my-project/ │ ├── node_modules/ ├── package.json ├── package-lock.json └── app.js It also adds Express to your package.json : { "dependencies" : { "express" : "^5.0.0" } } This means: Express belongs to this project. Anyone who clones your repository can simply run: npm install and npm installs everything automatically. That's exactly what you want for project dependencies. Why Local Installation Matters Imagine you're building an API. Your code contains: const express = require ( " express " ); Now imagine another developer clones your project. If Express was installed locally, they only need to run: npm install Everything works. If it wasn't, they'll see something like: Cannot find module 'express' because the dependency isn't part of the project. That's why libraries your application depends on should almost always be installed locally. Global Installation Now consider this command: npm install -g nodemon This installs node