57

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?

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
Zia Ur Rehman
  • 1,141
  • 2
  • 11
  • 20
  • 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. – thmshd May 29 '14 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 – Matej Feb 05 '16 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 – thmshd Feb 15 '16 at 07:14
  • I described a partial workaround based on symbolic links (Mac/Linux): [Huge number of files generated for every Angular project](https://stackoverflow.com/a/48628016/86967) – Brent Bradburn Apr 11 '20 at 15:26

3 Answers3

45

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 module.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.

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • 2
    npm's FAQ also addresses this pretty explicitly: https://www.npmjs.org/doc/faq.html#node_modules-is-the-name-of-my-deity-s-arch-rival-and-a-Forbidden-Word-in-my-religion-Can-I-configure-npm-to-use-a-different-folder and explains some of why this is. – pauljz Feb 17 '14 at 00:24
  • 21
    Damn these underscores! :( – Zia Ur Rehman Feb 17 '14 at 00:28
  • 1
    @ZiaUrRehman, it's just a convention. Everyone in the community is using it, so you'll just have to get used to it. – Mulan Feb 17 '14 at 00:29
  • 4
    Cached link to the old FAQ: https://web.archive.org/web/20150216184318/https://docs.npmjs.com/misc/faq#node_modules-is-the-name-of-my-deitys-arch-rival-and-a-forbidden-word-in-my-religion-can-i-configure-npm-to-use-a-different-folder – mbursill May 30 '16 at 01:41
  • npm FAQ link is broken. – jpmc26 Aug 14 '18 at 18:07
9

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.

Dirk R
  • 602
  • 7
  • 14
  • Would you put this in the user or project directory? –  Nov 11 '19 at 03:42
  • 1
    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. – Dirk R Nov 12 '19 at 08:23
  • 4
    how would this live in a `require` world ? – y_nk Nov 18 '19 at 10:06
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>
MPazik
  • 73
  • 4
  • 1
    Creating a .yarnrc files saves you the hassle of having to type in --modules-folder every time. See my answer below. – Dirk R Jan 01 '19 at 19:42
  • But its not working when executing Yarn run, it says package webpack not found. – Komal Jul 08 '21 at 07:00