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

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.


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.


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>