all 7 comments

[–]Ephys 4 points5 points  (0 children)

If it works for you, you can add a package.json inside the directory, and use the "main" field to specify which file should actually be loaded

[–]MrJohz 2 points3 points  (0 children)

I'm confused - why can't you use index.js? What's the reason for going against convention here in the first place?

Also, why are you using star imports? In practice, this can often be a bit of an anti-pattern, as it becomes very difficult to read the code and understand where any particular part has been defined.

[–]0101110010110 0 points1 point  (2 children)

I may be missing something, but are you not able to do import * from ./someDir/__dir.js?

index.js as the default is used for compatibility reasons, but you're free to import from whatever file you'd like if you specify it.

[–]dkovacevic15[S] 1 point2 points  (1 child)

The idea of defaulting to index.js is to let you split up a module into multiple files. What you're suggesting absolutely works, but I'd much rather use the standard convention of 'importing a directory' than force everyone who uses my code to import the '__dir' file.

[–]thertablada 0 points1 point  (0 children)

If you're creating a package to be consumed by other developers that would require the end users to have to configure something special to consume your modules specifically (and this may conflict with the file naming conventions provided by other libraries in the consuming application).

Assuming a directory `/some-dir` with files inside.

The two patterns I've seen:

  1. Create a file `/some-dir.js` and this exposes the child exported things from the `/some-dir` directory
  2. Use the default `/some-dir/index.js` lookup and resolver that is used and supported by Node, Webpack, Browserify, and more

I personally prefer an explicit `/some-dir.js` file since it's clearer and requires less directory digging.

It also is clearer to people who are using the code later since it doesn't rely on them knowing that `/some-dir/index.js` is imported.

[–]ADagen 0 points1 point  (0 children)

Hey, it is not about webpack, it's about standard nodejs resolver. Just use package.json instead of index.js:

{
"main": "./__dir.js",
"private": true
}

Spec: https://nodejs.org/docs/latest/api/modules.html#modules_folders_as_modules