Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't require('./') the same thing as require('./index') from inside a module that has an index.js?

Tags:

node.js

Been a way from Node for a bit and am finding myself confused about something I thought I understood:

From outside a module that has an index.js, if I require('path/to/module'), that index.js is consulted for that module's exports.

Now, I've got a situation where I'm working within such a module, and am trying to make references relative to the module's own index.js:

// index.js
exports.find_tests = require('./lib/find_tests');

// test.js
require('./').find_tests  // does not work
require('./index').find_tests  // works

I thought the first variant (require('./').find_tests) would work. Am I totally off base? I've got no main entry in package.json.

  • Accessing require('./') yields is an empty object. What is it?
  • require('./index') is exports in index.js.
like image 861
Dmitry Minkovsky Avatar asked Jan 20 '26 21:01

Dmitry Minkovsky


1 Answers

I'm not entirely sure on the "why" that it doesn't work as expected, but I can tell you that if you require('./.'), it will work as expected.

like image 65
Tim Caswell Avatar answered Jan 22 '26 11:01

Tim Caswell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!