Closed dalisoft closed 2 months ago
This is because, as it's an ESM import, we dont allow index.*
main files.
Imports require a full file path.
I assume you're putting your code through a transpiler that is incorrectly resolving this :thinking:
If I’m add /index suffix, it works but without /index suffix it does not working. It’s TypeScript codebase and you can see via link to repository
Yeah, so in this case, the import is technically missing. This is because you should have the full file url in the import (this includes the file extension).
This I believe is to match node to browsers more closely, as exact file paths are required in a browser environment.
The import style you're attempting to use in the legacy require
pattern with the esm import
.
You can see us patching require here (where we allow it to lookup files via the main
field in the package json, and searching for index
when a directory is provided):
https://github.com/eslint-community/eslint-plugin-n/blob/1bcb006e6d7ab4949a653d663801ee4dd9b0dc35/lib/util/import-target.js#L279-L286
We do attempt to compensate for typescript extensions here: https://github.com/eslint-community/eslint-plugin-n/blob/1bcb006e6d7ab4949a653d663801ee4dd9b0dc35/lib/util/import-target.js#L291-L294
Taking the mapping from here: https://github.com/eslint-community/eslint-plugin-n/blob/1bcb006e6d7ab4949a653d663801ee4dd9b0dc35/lib/util/get-typescript-extension-map.js#L21-L27
https://dev.to/ayc0/typescript-50-new-mode-bundler-esm-1jic
I was wondering you can safely disable the rule if using ts + bundler - ts will report an error if you are importing a non-existent module. 🤔
If it's ok then i close this issue. Code with /index.ts
works so no issue just looks a much different
Environment
Node version: v20.14.0 npm version: v10.7.0 ESLint version: v9.5.0 eslint-plugin-n version: v17.9.0 Operating System: macOS 14.5
What rule do you want to report?
n/no-missing-import
Link to Minimal Reproducible Example
https://github.com/nanoexpress/ultimate-oss/tree/refactor
What did you expect to happen?
Auto-choose
/index
without warning but results are belowParticipation
Additional comments
No response