Skip to content

Instantly share code, notes, and snippets.

@odellt
Created September 25, 2019 12:15
Show Gist options
  • Save odellt/34be9719eb1780dabc0cffb50259ddc6 to your computer and use it in GitHub Desktop.
Save odellt/34be9719eb1780dabc0cffb50259ddc6 to your computer and use it in GitHub Desktop.
Description of how Nodejs handles files and directories with the same name

Nodejs Require Module Resolution

// ./main.js
require('./example-module').whoami();

// ./example-module.js
module.exports.whoami = () => console.log(`Hello, I'm a file`);
// Output: ./example-module.js

// ./example-module/index.js
module.exports.whoami = () => console.log(`Hello, I'm an index.js file inside a directory`);
// Output: ./example-module/index.js

Executing the main.js file described above will output Hello, I'm a file as the file example-module.js is given precendence over a directory of the same name, when the require module attempts to resolve ./example-module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment