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
Enter fullscreen mode Exit fullscreen mode
and
npm install -g package-name
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
It also adds Express to your package.json:
{
"dependencies": {
"express": "^5.0.0"
}
}
Enter fullscreen mode Exit fullscreen mode
This means:
- Express belongs to this project.
- Anyone who clones your repository can simply run:
npm install
Enter fullscreen mode Exit fullscreen mode
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");
Enter fullscreen mode Exit fullscreen mode
Now imagine another developer clones your project.
If Express was installed locally, they only need to run:
npm install
Enter fullscreen mode Exit fullscreen mode
Everything works.
If it wasn't, they'll see something like:
Cannot find module 'express'
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
This installs nodemon globally on your computer.
Instead of living inside a project, it's available from any terminal.
You can now run:
nodemon app.js
Enter fullscreen mode Exit fullscreen mode
from any project on your machine.
Global packages are mainly meant for command-line tools (CLI tools) rather than libraries used by your application.
Common Global Packages
Some examples include:
nodemontypescriptnpmpm2-
eslint(if you prefer using it globally)
These provide commands that you use while developing rather than becoming part of your application's runtime.
A Common Beginner Mistake
Many beginners do this:
npm install -g express
Enter fullscreen mode Exit fullscreen mode
It works on their machine because Express is installed globally.
Then they push their code to GitHub.
Another developer clones the repository and gets:
Error: Cannot find module 'express'
Enter fullscreen mode Exit fullscreen mode
Why?
Because global packages are not shared with your project.
They only exist on your own computer.
The correct way is:
npm install express
Enter fullscreen mode Exit fullscreen mode
Now Express becomes part of the project and is recorded in package.json.
So When Should You Install Globally?
Ask yourself one simple question:
Is this package used by my project, or by me?
If the project needs it:
âś… Install locally
npm install package-name
Enter fullscreen mode Exit fullscreen mode
If you need the command across many projects:
âś… Install globally
npm install -g package-name
Enter fullscreen mode Exit fullscreen mode
This simple rule avoids most confusion.
What About npx?
Modern Node.js development has made global installations less necessary.
Instead of installing a CLI globally, you can often use:
npx nodemon app.js
Enter fullscreen mode Exit fullscreen mode
or
npx create-react-app my-app
Enter fullscreen mode Exit fullscreen mode
npx will use the local package if it's already installed, or temporarily fetch and execute it when appropriate.
That means:
- No global installation
- No version conflicts between projects
- Cleaner development environment
This is why you'll see npx recommended in many official tutorials.
Quick Comparison
| Local Installation | Global Installation |
|---|---|
| Inside current project | Installed on your computer |
Saved in package.json
|
Not saved in project |
| Used by project code | Used by developers |
| Shared with team | Personal to your machine |
Installed with npm install
|
Installed with npm install -g
|
My Rule of Thumb
Whenever I'm unsure, I remember this:
- 📦 If my application needs it → Install locally
- 🖥️ If I need the command everywhere → Install globally
- ⚡ If I only want to run it once → Use
npx
This simple mindset has helped me avoid a lot of confusion while learning Node.js.
Final Thoughts
Understanding the difference between local and global packages isn't just about memorizing commands—it's about understanding who the installation is for.
- Your application depends on local packages.
- You, the developer, benefit from global CLI tools.
- And
npxgives you a convenient way to run many tools without installing them globally.
It's a small concept, but getting it right makes working with Node.js projects much smoother.
Thanks for reading! đź‘‹
I'm currently documenting my Node.js learning journey through the Backend Internals series. If you're learning Node.js too, feel free to connect—I hope these notes make your journey a little easier.
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.