Can a custom directory name be used instead of 'node_modules' when installing node packages?

node.jsNpmConfig

node.js Problem Overview


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?

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

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.

Solution 3 - node.js

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZia Ur RehmanView Question on Stackoverflow
Solution 1 - node.jsloganfsmythView Answer on Stackoverflow
Solution 2 - node.jsDirk RView Answer on Stackoverflow
Solution 3 - node.jsMPazikView Answer on Stackoverflow