68

Is there a way to change the node_modules folder location?

For example:

- dir1
- dir2
- node_modules

to:

- dir1
- dir2
    - node_modules
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
M K
  • 9,138
  • 7
  • 43
  • 44
  • 5
    I guess you are looking for a way to install dependencies in a different folder, right? If so, all you have to do move your `package.json` file inside `dir2` then run `npm install` from dir2. – Kamrul Sep 24 '13 at 07:33
  • @Kamrul but then I would have to run the application form the dir2? – M K Sep 24 '13 at 07:58
  • @Kamrul it did the trick for me! Just had to use a shell script to start the server, and not the default npm start. Please, post it as an answer so I can accept it. – M K Sep 24 '13 at 08:05
  • @maximkott: you read this statement from answer. `"It will start in the current directory and then work its way up the folder hierarchy, checking each level for a node_modules folder. Once Node.JS finds the node_modules folder, it will then attempt to load the given module"` – Amol M Kulkarni Sep 25 '13 at 17:54
  • 1
    @AmolMKulkarni sorry, I've must have missed it. Thanks! :) – M K Sep 26 '13 at 09:31
  • @maximkott: Anyways everyone is trying to help each other, Happy to see you got your problem resolved.. – Amol M Kulkarni Sep 26 '13 at 09:44
  • 2
    @AmolMKulkarni Yay! I'm falling in love with node! So powerful and flexible - it's amazing! – M K Sep 26 '13 at 13:58

2 Answers2

56

The following is the code which looks int the node_modules folder by default

Module.prototype.load = function(filename) {
  debug('load ' + JSON.stringify(filename) +
        ' for module ' + JSON.stringify(this.id));

  assert(!this.loaded);
  this.filename = filename;
  this.paths = Module._nodeModulePaths(path.dirname(filename));

  var extension = path.extname(filename) || '.js';
  if (!Module._extensions[extension]) extension = '.js';
  Module._extensions[extension](this, filename);
  this.loaded = true;
};

So, following is the exact search pattern:

  1. Node.JS looks to see if the given module is a core module. (e.g. http, fs, etc.) Always takes the precedence in the loading modules.

  2. If the given module is not a core modules(e.g. http, fs, etc.), Node.js will then begin to search for a directory named, node_modules.

    It will start in the current directory (relative to the currently-executing file in Node.JS) and then work its way up the folder hierarchy, checking each level for a node_modules folder. Once Node.JS finds the node_modules folder, it will then attempt to load the given module either as a (.js) JavaScript file or as a named sub-directory; if it finds the named sub-directory, it will then attempt to load the file in various ways. So, for example

  3. If you make a request to load the module, "utils" and its a directory not a .js file then:
    Node.JS will search a hierarchical directory for node_modules and utils in the following ways:

    ./node_modules/utils.js
    ./node_modules/utils/index.js
    ./node_modules/utils/package.json
    
  4. If Node.JS still can't find the file in above steps, Node.js will then start to look into the directory paths from environment variables i.e. NODE_PATH set on your machine(obviously set by Node.JS installer file if you are on windows) Not Found in all the above steps then, prints a stack trace to stder
    E.g.: Error:Cannot find module 'yourfile'
    For more information: link is here even the cyclic require() is explained very well..

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164
  • 6
    Thank you very much for this nice explanation. So putting it all together - there is no easy way to do that / or shouldn't be done at al, due to compatibility to other modules? – M K Sep 24 '13 at 06:42
  • 1
    You can go for relative path with your `require('./pathFolder/PathFile');` – Amol M Kulkarni Sep 24 '13 at 06:43
  • 1
    But that is not what I want to achieve. All I wanted is to place the node_modules folder somewhere else. – M K Sep 24 '13 at 06:48
  • 1
    This is not an answer for the question. – randunel Jan 16 '14 at 22:03
  • @randunel: The question was answered indirectly with the information provided. How do you say `This is not an answer for the question`. I cannot answer in `YES` or `NO` to the question asked right? It makes no sense. – Amol M Kulkarni Jan 17 '14 at 09:07
  • 4
    I came here looking for an easier way to change the `node_modules` folder location or name, and the only (and accepted) answer I find is for `How to find out where npm is looking for node_modules`. Can you spot the difference between the two questions? Your answer si an answer, but not one for this question. – randunel Jan 17 '14 at 09:55
  • @randunel: It is node which looks for `node_modules` folder (Not `npm`; npm is just package manager for node.js). And with the answer, If you know all the ways how the modules are loaded into your node program, then you can easily figure out the answer for this particular question. – Amol M Kulkarni Jan 17 '14 at 11:04
  • Why assume that i am trying to load something from the `node_modules` folder, which is done by node? Perhaps I made a mistake trying to phrase a question for your answer, I am not native English :) I'm trying to install a package in a different directory, not in `node_modules` :P I know how the modules are loaded into an application, I just wanted an easier way to change the `node_modules` path into `my/custom/path` dir when using npm, without hacking npm :D This question is similar to my issue, but the answer does not apply :( – randunel Jan 17 '14 at 22:17
  • 2
    @randunel Please see this [npm FAQs](https://npmjs.org/doc/faq.html#node_modules-is-the-name-of-my-deity-s-arch-rival-and-a-Forbidden-Word-in-my-religion-Can-I-configure-npm-to-use-a-different-folder), then you will then understand how many hurdles are there to do so(as you wanted). – Amol M Kulkarni Jan 20 '14 at 13:12
  • 2
    @randunel Your issue is not actually covered by this particular question, since he wanted to keep same named folder (i.e. `node_modules`) in different locations. So, Let him understand where and all the node looks for `node_modules` folder. Answer for your question would be "Never going to happen like `my/custom/path`. The folder is named `node_modules`. It is written indelibly in the Node Way, handed down from the ancient times of Node 0.3." – Amol M Kulkarni Jan 20 '14 at 13:18
  • 1
    Thanks for the detail explaination – Zorji Jan 31 '15 at 08:37
  • I really don't believe, someone down voted this today :| Please consider commenting your problem (which will obivously help others too) :p – Amol M Kulkarni Feb 18 '16 at 11:08
0

In fact, we can even edit the nodevars.bat file in which the last line is:

if "%CD%\"=="%~dp0" cd /d "%HOMEDRIVE%%HOMEPATH%"

We can specify our own directory instead of looking after the user's home directory:

%HOMEDRIVE%%HOMEPATH%

so that node-modules-location will automatically looked after.

VLS
  • 2,306
  • 4
  • 22
  • 17
mohu
  • 79
  • 1
  • 4