require/resolve and the exclusion of the node_modules folder

30 views
Skip to first unread message

Bernhard Schelling

unread,
Sep 29, 2015, 7:45:59 PM9/29/15
to nodejs
Hi,

looking at the node documentation i can't quite figure out how the resolve works

i have index.js requiring a domhandler via 

  DomHandler = require("domhandler");


full path to index is
/node_modules/htmlparser2/lib/index.js 

thie domhandler directory is found in /node_modules/htmlparser2/node_modules/domhandler, it contains
a package.json pointing to the index.js file in the same directory.

If I read the documentation right, the described function node_module_path would never give me the path to 
the domhandler directory because "node_module" is not added to the directory array - the pseudo code never 
adds a part to the directory entry if its name is node_modules (see code below or at https://nodejs.org/api/modules.html)

the code would filter all the node_modules folder from the directory pathes and I would end up with pathes like

/node_modules/
/htmlparser2/node_modules/
/htmlparser2/lib/node_modules

is this a problem of the documentation or am I missing something?
I assume that " path split(START) " would give me an Array like ["node_modules", "htmlparser2", "lib"]

thanks for hints

bernhard



NODE_MODULES_PATHS(START)
1. let PARTS = path split(START)
2. let I = count of PARTS - 1
3. let DIRS = []
4. while I >= 0,
   a. if PARTS[I] = "node_modules" CONTINUE
   c. DIR = path join(PARTS[0 .. I] + "node_modules")
   b. DIRS = DIRS + DIR
   c. let I = I - 1
5. return DIRS

Ryan Graham

unread,
Sep 29, 2015, 11:06:20 PM9/29/15
to nodejs
Here's a (possibly over) simplified description:

If given a path (starting with ../, ./, or /), the path is used as is, relative to the current file.

If a module-name is given, it basically looks for:
./node_modules/<module-name>
../node_modules/<module-name>
../../node_modules/<module-name>
../../../node_modules/<module-name>
...
/node_modules/<module-name>

Once a matching folder is found it looks for:
package.json, then uses the 'main' property to determine the file to load, relative to the package.json
if not given, looks for index.js in the package root.

There's slightly more to it, and some additional cases when it comes to referencing modules by path, but that's the basic idea.

~Ryan

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/3c14be12-9936-40c9-8268-39d4a85f052a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages