Is there a way to move the node_modules directory in an application to let's say /vendor/node_modules like bower does with the bowerrc file? I thought it could be specified in package.json but I can't seem to find a solution. Your help is much appreciated.
What exactly is the node_modules folder and what is it for? It just a directory created by npm and a way of tracking each packages you install locally via package.
You can if you'd like, but it is not considered best practice. In any case, you shouldn't make any modifications to the files inside node_modules . Preferably you need to only copy over your package. json file and optionally, your package-lock.
Node will look for your modules in special folders named node_modules . A node_modules folder can be on the same level as the current file, or higher up in the directory chain. Node will walk up the directory chain, looking through each node_modules until it finds the module you tried to load.
yes you can, just set the NODE_PATH env variable :
export NODE_PATH='yourdir'/node_modules
According to the doc :
If the NODE_PATH environment variable is set to a colon-delimited list of absolute paths, then node will search those paths for modules if they are not found elsewhere. (Note: On Windows, NODE_PATH is delimited by semicolons instead of colons.)
Additionally, node will search in the following locations:
1: $HOME/.node_modules
2: $HOME/.node_libraries
3: $PREFIX/lib/node
Where $HOME is the user's home directory, and $PREFIX is node's configured node_prefix.
These are mostly for historic reasons. You are highly encouraged to place your dependencies locally in node_modules folders. They will be loaded faster, and more reliably.
Source
In short: It is not possible, and as it seems won't ever be supported (see here https://github.com/npm/npm/issues/775).
There are some hacky work-arrounds with using the CLI or ENV-Variables (see the current selected answer), .npmrc-Config-Files or npm link
- what they all have in common: They are never just project-specific, but always some kind of global
Solutions.
For me, none of those solutions are really clean because contributors to your project always need to create some special configuration or have some special knowledge - they can't just npm install
and it works.
So: Either you will have to put your package.json in the same directory where you want your node_modules installed, or live with the fact that they will always be in the root-dir of your project.
Yarn supports this feature:
# .yarnrc file in project root
--modules-folder /node_modules
But your experience can vary depending on which packages you use. I'm not sure you'd want to go into that rabbit hole.
I'm not sure if this is what you had in mind, but I ended up on this question because I was unable to install node_modules
inside my project dir as it was mounted on a filesystem that did not support symlinks (a VM "shared" folder).
I found the following workaround:
package.json
file to a temp folder on a different filesystemnpm install
therenode_modules
directory back into the project dir, using cp -r --dereference
to expand symlinks into copies.I hope this helps someone else who ends up on this question when looking for a way to move node_modules
to a different filesystem.
There is another workaround, which I found on the github issue that @Charminbear linked to, but this doesn't work with grunt
because it does not support NODE_PATH
as per https://github.com/browserify/resolve/issues/136:
lets say you have
/media/sf_shared
and you can't install symlinks in there, which means you can't actually npm install from/media/sf_shared/myproject
because some modules use symlinks.
$ mkdir /home/dan/myproject && cd /home/dan/myproject
$ ln -s /media/sf_shared/myproject/package.json
(you can symlink in this direction, just can't create one inside of /media/sf_shared)$ npm install
$ cd /media/sf_shared/myproject
$ NODE_PATH=/home/dan/myproject/node_modules node index.js
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With