It's worth noting how Node.js goes about resolving a particular required module. For example, if a file /home/tony/projects/foo.js has a require call require('bar'), Node.js scans the filesystem for node_modules in the following order. The first bar.js that is found is returned:
- /home/tony/projects/node_modules/bar.js
- /home/tony/node_modules/bar.js
- /home/node_module/bar.js
- /node_modules/bar.js
Node.js looks for node_moduels/bar in the current folder followed by every parent folder until it reaches the root of the filesystem tree for the current file.
The module foo/index.js can be required as foo, without specifying index, and will be picked by default.