Specify path to node_modules in package.json
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.
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