There
are a few ways in which a folder may be passed to require()
as an
argument.
package.json
file in the root of the
folder, which specifies a main
module.
An example package.json
file
might look like this:
{ "name" : "some-library", "main" : "./lib/some-library.js" }
If this was in a folder at
./some-library
, then require('./some-library')
would attempt
to load ./some-library/lib/some-library.js
.
This is the extent of Node's awareness of package.json
files.
package.json
file present in the directory, then node will
attempt to load an
index.js
or
index.node
file out of that directory.
For
example, if there was no
package.json
file in the above example, then
require('./some-library')
would attempt to load:
./some-library/index.js ./some-library/index.node
Casiano Rodriguez León 2015-01-07