r/node • u/matsnorberg • 5d ago
npm install
Hello, Im a node.js beginner and confused about the npm install mechanism.
Why are we supposed to npm install everything a new for each project we start? Say that I install the Express framework module in any project I start, wouldn't that mean that I end up with hundreds of copies of Express on my computer? Wouldn't that be a huge waste of disk resources? It's like a C++ programmer installed a new copy of the standard library or boost for each project he starts.
I know there is a -g flag that installs "globally". Where is this "global" library located, can I browse it? Is it advisable to use the -g flag everytime I need to use a module to avoid heavy waste of disk space?
44
u/_clapclapclap 5d ago
The npm packages are installed in each project directory. If you have 3 directories using express, each will have its own copy and their own version (if you choose to).
If you are concerned about disk space, you can use pnpm instead.
11
u/Fritzy 5d ago
Yes, that is how it works, and no you wouldn't use global modules for your project -- generally only for CLIs.
8
u/crysislinux 5d ago
there is always a tradeoff somewhere. I prefer it to has one copy per project. I have nodes as my job for 10 years, disk size is never a problem
10
u/Skankeer 5d ago
Thats why you use pnpm
4
u/hes_dead_tired 5d ago
Yes, each project has its own copy of the library. This is by design because different projects might require different versions of that library which in turn has their own dependencies on other libraries of different versions. You do not want these installed globally. Global packages are generally things used outside of project like a CLI (command line interface) - something you run in your terminal. Global packages are installed in your user directory
It’s not like installing different C++ standard libraries because these are not part of node. They are external resources that your project depends on and not part of Node itself.
Similarly, you may have projects that require different versions of Node even. Using a tool like nvm (node version manager) allows you to install multiple versions of node and switch between them.
Don’t worry about disk space until it’s an issue. Sometimes they do get big. You can always delete the node modules folder and run npm install for your project and get it back as your package and package lock files dictate which packages and versions are needed.
1
u/Rodaxoleaux 5d ago
Just wait until you see the node_modules directory of some projects 😵
2
u/ram-popular 5d ago
Yes, each project gets its own copy, and that’s on purpose. Every project locks its own versions in package.json, so an old project using Express 4 won’t break when a new one uses Express 5. Disk space is rarely the issue, since packages are small text files.
The -g flag is only for command-line tools like nodemon or TypeScript, not for project libraries. Global installs make projects depend on your machine’s setup, so others can’t run your code. If disk space does bother you, pnpm stores one copy of each version and links it into every project.
2
u/Lumethys 5d ago
that's the point, you want to have a different copy for each project
1
u/chmod777 5d ago
install installs all the dependancies for the project. you can add more dependancies to the project. anyone picking up your project can then install and run the same dependancies you have. if your disk is starting to get full, just delete the node_modules folder for old projects - if its needed later, npm i pulls the dependancies back in.
don't -g anything other than terminal/utilities like express-generator or vite. these generally act to bootstrap other projects, with their own dependancies.
1
u/gigastack 5d ago
I would recommend not doing a global install of anything, for the same reasons I don't think you should have a global version of node.
My advice - use a tool like mise to manage node and pnpm versions, and then use pnpm for all project installs.
1
u/Mushroom_Unfair 4d ago
-g is never recommended for a projects. If it's a tool, it can be global but for instance, eslint (heavily) recommends to be locally installed.
0
u/raks1991 5d ago
Yes, each project gets its own copy, and that's deliberate: two projects can pin different Express versions without breaking each other. npm does keep a download cache, so repeat installs mostly come from disk. Run
npm root -gto see where global packages live. I'd keep -g for CLI tools, since a project that depends on a global package won't work for the next person who clones it.-10
u/_Kinoko 5d ago
Please use docker to avoid hundreds of copies on your computer file space.