Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 872
Zia Ur Rehman Avatar asked Feb 17 '14 00:02

Zia Ur Rehman


People also ask

Does npm install create a node_modules folder?

When you first install a package to a Node. js project, npm automatically creates the node_modules folder to store the modules needed for your project and the package-lock. json file that you examined earlier. The node_modules folder contains every installed dependency for your project.

Is it mandatory to commit the node_modules folder in your application?

Not committing node_modules implies you need to list all your modules in the package. json (and package-lock. json ) as a mandatory step. This is great because you might not have the diligence to do so, and some of the npm operations might break if you don't.

Is node_modules required?

No, everything is in the bundle after you build. You can take the files defined as output (usually whatever is in the "dist" folder) and stick them on whatever static server you want without the need of the supporting node_modules .

When install node packages it gets created in the folder called?

On Windows, it's %AppData%\npm . On Unix systems, it's one level up, since node is typically installed at {prefix}/bin/node rather than {prefix}/node.exe . When the global flag is set, npm installs things into this prefix.


3 Answers

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.

like image 57
loganfsmyth Avatar answered Oct 20 '22 05:10

loganfsmyth


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.

like image 32
Dirk R Avatar answered Oct 20 '22 05:10

Dirk R


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>
like image 4
MPazik Avatar answered Oct 20 '22 04:10

MPazik