Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move away from index.js file names on non-index files #481

Open
lgtmak opened this issue Mar 26, 2025 · 4 comments
Open

Move away from index.js file names on non-index files #481

lgtmak opened this issue Mar 26, 2025 · 4 comments

Comments

@lgtmak
Copy link

lgtmak commented Mar 26, 2025

There are several files that are built in dist/ that contain an index.js file. For example: dist/module/index.js

These files however is for a the Index models and the files should be renamed _index.js or db.js something more sensible that will not automatically be imported on require/import.

Why? It conflicts with the CommonJS import system:

https://nodejs.org/api/modules.html#all-together

LOAD_INDEX(X)
1. If X/index.js is a file
    a. Find the closest package scope SCOPE to X.
    b. If no scope was found, load X/index.js as a CommonJS module. STOP.
    c. If the SCOPE/package.json contains "type" field,
      1. If the "type" field is "module", load X/index.js as an ECMAScript module. STOP.
      2. Else, load X/index.js as a CommonJS module. STOP.
2. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
3. If X/index.node is a file, load X/index.node as binary addon. STOP

Some other good reading: https://dev.to/fahadaminshovon/-how-to-use-indexjs-fileproperly-302f

@ts-thomas
Copy link
Contributor

Done, except the src/index.js which follows the main naming schema of all flexsearch exports, otherwise it needs a different term for this

@lgtmak
Copy link
Author

lgtmak commented Mar 26, 2025

fs_index? idx?

@ts-thomas
Copy link
Contributor

There are more than 300 occurences in my whole project folder which points to this file. This can't be done within a minor release cycle. Do you have a small example I can use to reproduce this issue?

@lgtmak
Copy link
Author

lgtmak commented Mar 28, 2025

The module is already working well enough for me but if a project requires specific imports then a developer may trip up on it.

If I import/require:

import FlexSearch from 'flexsearch/src';

The expectation in the Node CommonJS world is that FlexSearch will be an object containing the exports of src/ directory as facilitated by index.js import sequence and the common developer practice of exporting directories in index.js files.

For example if src/index.js looked like this, the above import assumption would align with the standard:

module.exports = {
  Index,
  Document,
 ...
}

Instead with current code when I import flexsearch/src I only get the FlexSearch Index model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants