Zia Ur Rehman
Zia Ur Rehman

Reputation: 1181

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

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?

Upvotes: 60

Views: 33431

Answers (3)

loganfsmyth
loganfsmyth

Reputation: 161447

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.

Upvotes: 48

Dirk R
Dirk R

Reputation: 739

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.

Upvotes: 10

MPazik
MPazik

Reputation: 73

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>

Upvotes: 4

Related Questions