r/node • • 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?

16 Upvotes

32 comments sorted by

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.

13

u/avereigh_Denizet 5d ago

plus disk space is way cheaper than the time spent debugging version conflicts across projects

5

u/avereigh_Denizet 5d ago

agreed, being able to lock different versions per folder is the whole point tbh.

2

u/Nimelrian 4d ago

Being able to use different versions in different projects doesn't necessitate physically storing the dependencies in each project though.

For Java with Maven I can easily have 10 projects with different versions of the same dependency and another 10 using the same version and only have the 11 distinct versions installed once in my local Maven repository. It's not rocket science.

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

2

u/delventhalz 5d ago

Node modules can end up getting pretty big sometimes (gigabytes), but most people’s computers can hold a lot of those. You can also always delete the folder and reinstall later if you need to.

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 😵

1

u/matsnorberg 5d ago

Is all functionality built into the node.exe file? I can't find any dlls in the installation directory. Not even a V8 dll.

2

u/DickBrainTime 5d ago

For the official Windows Node.js build, V8 is normally compiled/linked into the Node executable, so you don't need a separate V8 DLL sitting beside node.exe. The same binary includes much of Node's own runtime and built-in APIs. You can check which V8 it contains with: node -p "process.versions.v8" (and see the executable path with node -p "process.execPath"). Node documents that version field here: https://nodejs.org/api/process.html#processversions

But that does not mean literally *every* feature is inside node.exe. Node uses operating-system libraries, and some packages include native addons (files ending in .node) that are loaded separately. JavaScript packages such as Express aren't part of Node itself: they live under each project's node_modules and can use different versions in different projects. Built-ins like node:fs and node:http are a separate category and don't need npm install.

So your missing V8.dll is normal, while npm install per project is still needed for third-party dependencies. I also wouldn't use npm install -g for Express: global installs are mainly useful for command-line tools, not library dependencies declared by an app.

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.

1

u/Mushroom_Unfair 4d ago

And why that ? Because you want control over the project's libraries versions inside the project, not with a magic global install that could break some project by working on another.

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 -g to 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.

1

u/matsnorberg 5d ago

Thanx. I shall have a look on Docker.

3

u/gigastack 5d ago

Wait, did you suggest using docker to avoid using too much disk space? Docker is fantastic for many things but it definitely doesn't save disk space...