To understand module resolution, see Module Documentation , especially Downloading from the node_modules
Folders .
For example, if the file in '/home/ry/projects/foo.js'
is called require('bar.js')
, then the node will look in the following places in the following order:
/home/ry/projects/node_modules/bar.js
/home/ry/node_modules/bar.js
/home/node_modules/bar.js
/node_modules/bar.js
NPM uses this by installing modules in:
./node_modules/{module}
So, when you use npm install formidable
, it will create and install the module in:
./node_modules/formidable
But this means that only scripts in the current directory, including subdirectories, will succeed in using require('formidable')
:
./foo.js ./lib/bar.js ./src/baz.js ./src/sub/qux.js
However, you can install modules as "global", but you must explicitly request it with -g
or --global
:
npm install -g formidable
Then any script on the system must be able to require('formidable')
.
As for tree output, you have 5 installed modules available from the current directory:
express
formidable
node-inspector
npm
socket.io
Everything else in the tree is a list of the dependencies of these modules, their dependencies, etc., but only these 5 are available for require(...)
inside your scripts.