Closed yolapop closed 8 years ago
@yolapop Hi, thanks for the great question.
Isn't it needed to add ./ in require to find local modules?
Yes, you are absolutely correct here. Now that you point that out, I think I should probably update that section as it is currently inaccurate.
Node.js will perform a hierarchical directory search for "node_modules" and "utils" in the following ways:
./node_modules/utils.js ./node_modules/utils/index.js ./node_modules/utils/package.json
I don't think this is technically accurate either... the directory search would look like this instead:
./node_modules/utils/package.json
../node_modules/utils/package.json
.../node_modules/utils/package.json
etc until it reaches /
utils
with a package.json
in it inside of a node_modules
it will read the package.json
and check and see if a main
property is set. for example, you could set your main
to be called utils.js
. otherwise if main
is not specified it will default to looking for index.js
utils
module and the name of the main entry file, it will require
the main entry filehope this helps!
@yolapop also I can highly recommend this nodeschool workshop https://github.com/npm/how-to-npm
Thanks for the answer and recommendation!
On Sun, Mar 22, 2015, 11:41 PM Max Ogden notifications@github.com wrote:
@yolapop https://github.com/yolapop also I can highly recommend this nodeschool workshop https://github.com/npm/how-to-npm
— Reply to this email directly or view it on GitHub https://github.com/maxogden/art-of-node/issues/51#issuecomment-84651470.
Hi, I'm a beginner, just a couple of questions :)
Isn't it needed to add
./
inrequire
to find local modules? i.e. if I havefilter.js
in my current directory and I want to load it as a module I wouldrequire('./filter')
.Also, reading this http://www.bennadel.com/blog/2169-where-does-node-js-and-require-look-for-modules.htm
I'd like some clarification because the explanation above seems different with yours. Thank you.