62

I create a package.json, I run npm install, it works alright. It creates a node_modules directory in my root folder (which I can change by using --prefix option). However, I don't like underscores all that much. I want to change the name of the directory NPM downloads the modules to. I want it to be named nmods or node-modules or something like that.

Bower can do a similar thing by reading the directory property inside a .bowerrc file in the current dir. Is there a way to do the same with NPM?

4
  • 2
    Absolutely! Plus, the quite long name also sometimes breaks the file path limit in windows due to the fact that the node_modules dependencies are recursed over and over again. A shorter name like nmods would heavily reduce that risk... Oh my. Commented May 29, 2014 at 11:44
  • However as one would argue @thomasjaworski.com, it could be a windows bug, not a node/npm bug. This was heavily discussed on a github issue (can't find atm) so im not going to argue ;). Just saying :P Commented Feb 5, 2016 at 19:45
  • 1
    Agree @codeninja I know which github thread you mean, and I feel neither is a good excuse :) Windows totally failed with path length limitation, and the nodejs guys did not do any better by choosing this miserable convention Commented Feb 15, 2016 at 7:14
  • I described a partial workaround based on symbolic links (Mac/Linux): Huge number of files generated for every Angular project Commented Apr 11, 2020 at 15:26

3 Answers 3

49

There is no way to change it. The node_modules folder is actually not specific to NPM, it is part of Node's core module loading system. Seen here in modules/cjs/loader.js.

Changing it globally as you've mentioned would also potentially break some of the modules you are using too, as modules are sometimes packages with their dependencies already present in node_modules and changing it would cause that to break.

Sign up to request clarification or add additional context in comments.

5 Comments

npm's FAQ also addresses this pretty explicitly: npmjs.org/doc/… and explains some of why this is.
Damn these underscores! :(
@ZiaUrRehman, it's just a convention. Everyone in the community is using it, so you'll just have to get used to it.
npm FAQ link is broken.
11

Yarn you can easily achieve this by adding a file called '.yarnrc' with contents like this:

# ./.yarnrc
--modules-folder lib

Next time you run 'yarn' it will create the lib folder and install packages into there instead of into node_modules.

Now if only we could get 'npm install' to be as clever.

3 Comments

Would you put this in the user or project directory?
It should be placed in the same folder as where the node_modules folder gets created. This is should be the same folder where you run your yarn commands from. This is usually the project directory. The exception is if you usually run yarn off a subfolder of the project, then you would place the .yarnrc folder in there rather.
how would this live in a require world ?
4

There is no way to change it in npm, however, there is an option to configure it in yarn package manager.

yarn install --modules-folder <path>

2 Comments

Creating a .yarnrc files saves you the hassle of having to type in --modules-folder every time. See my answer below.
But its not working when executing Yarn run, it says package webpack not found.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.