1

Lets say, I have one entry.js file entry.js

var t = require('./test'); //it can be file or folder module

If I have a folder and a file named "test"in the same package as that of entry.js, how the execution will happen as both have the same name? which module has the highest priority?

0

2 Answers 2

2

The checking priority will be next:

  1. ./test.js
  2. ./test.json
  3. ./test.node
  4. ./test/index.js
  5. ./test/index.node

Sources:

https://nodejs.org/api/modules.html#modules_file_modules https://nodejs.org/api/modules.html#modules_folders_as_modules

Sign up to request clarification or add additional context in comments.

Comments

0

Frome reqire's docs:

Once Node.js finds the node_modules folder, it will then attempt to load the given module either as a (.js) Javascript file or as a named sub-directory.
And, if it finds the named sub-directory, it will then attempt to load the file in various ways. So, for example, if you make a request to load the module, "test":

var utils = require( "test" );

... Node.js will perform a hierarchical directory search for "node_modules"
and "test" in the following ways:

./node_modules/test.js
./node_modules/test/index.js
./node_modules/test/package.json

So, answer is: File first, Directory second, Module third.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.