I’m working on a JavaScript project that I pulled from a repository. After running npm install, I noticed something strange. Even though I already have some packages like lodash and express installed globally, npm downloaded them again in the project’s local node_modules folder. This seems unnecessary and is taking up space and time.
const express = require('express');
const _ = require('lodash');
const app = express();
const users = [{ name: 'Alice' }, { name: 'Bob' }];
app.get('/users', (req, res) => {
const userNames = _.map(users, 'name');
res.json(userNames);
});
Is this how npm is supposed to work? It doesn’t seem efficient to have duplicate installations. Is there a way to let npm use the globally installed packages instead?
Had this exact confusion when I started with Node a few years back. Global and local packages do totally different things. Global installs are for CLI tools - stuff like nodemon, webpack-cli, or create-react-app that you want to run from anywhere. Local installs are what your code actually imports. Even with express installed globally, your require statements check local node_modules first anyway. This keeps your project self-contained so it won’t break if someone else doesn’t have your global packages. Yeah, disk space adds up, but modern dev prioritizes reliability over storage. Your project works the same whether it’s on a fresh server or shared with other devs.
This is actually by design and there’s a good reason for it. Global packages are meant for command-line tools, not project dependencies. When Node.js looks for modules, it checks local node_modules folders first before checking global installations. This way your project uses the exact versions from package.json, which keeps things consistent across different dev environments. If you relied on global packages, your code might work fine on your machine but break when a teammate or production server has different global versions. Local installation prevents those ‘works on my machine’ problems and guarantees your builds will be reproducible. Sure, it takes up more disk space, but that’s worth it for stable, portable projects.
yup, that’s how npm rolls! each proj needs its own package versions to avoid conficts. your global lodash might be outdated or not fit the proj specs, so npm just grabs the right version. nothing to stress about!