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.
5 Answers
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.
-
1That is great. So in my case where I have local node_modules to each application I'm building, would I do that export in a js file in the project?– sturoidCommented Oct 10, 2014 at 6:26
-
3nope, it's an environment variable, it's setup at system-level. So you have to type it in your terminal. env variables are accessed within a node app in
process.env
– xShiraseCommented Oct 10, 2014 at 6:28 -
i try to run that in Windows and it doesnt work, it says:
'export' is not recognized as an internal or external command, operable program or batch file.
, it worked for me in OS X. Commented Oct 1, 2015 at 4:33 -
148I don't really think this is the answer to the question: He was asking for a property within the package.json to put a path for LOCAL (application level) npm-Packages. This answer tells him how he can move the GLOBALLY (-g) installed packages into another folder. Commented Mar 6, 2016 at 9:55
-
There is no word "property" in question or its title. Author write
I thought it could
be in package.json. But you can specify env variable inpackage.json
at script run command like this:{"scripts": { "start": "NODE_PATH='yourdir'/node_modules node ./your-app.js"}}
You also can use all expand ability of your shell.– oklasCommented Dec 10, 2021 at 15:34
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.
-
23"If you want to spell "node_modules" differently, sorry, no can do. That's built into node-core. Changing that is tantamount to building a completely different package manager." <== good job using variables I guess. Commented Jan 6, 2017 at 15:26
-
4A comment from 2019: it looks like yarn supports overriding the modules directory. Well, yarn is a new package manager, I suppose.– VanuanCommented Mar 5, 2019 at 11:02
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:
- Copy the
package.json
file to a temp folder on a different filesystem - Run
npm install
there - Copy the resulting
node_modules
directory back into the project dir, usingcp -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.
Other options
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
What if I am inside docker and I want to persist the node_modules
in a volume? Then, having the node_modules
inside of my app path is not an option for me.
When I deploy new code into the container, I put a tar into it, extract it to a temporary folder and then rsync from that to the app directory. I have the --delete
flag set for rsync, so everything that's not in the tar is removed from the app folder. That also applies to the node_modules
folder, so unfortunately the only solution that worked for me is the following:
Create a symbolic link in the app directory to a directory that contains the
node_modules
and is mounted into the container from the host.
node_modules
inpackage.json
, and xShirase's answer simply doesn't provide a way to do that.