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

On Tue, 29 Sep 2015 at 16:45 Bernhard Schelling <
bernhard.schell...@lemon42.com> wrote:

> 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 - 13. 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 - 15. return DIRS
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to nodejs@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/3c14be12-9936-40c9-8268-39d4a85f052a%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/3c14be12-9936-40c9-8268-39d4a85f052a%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAGjmZGzeNQGqCPH2Hbiy8z%2B1mZTF9aPL%3DYOKb7NYmFeEocS0%3Dg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to