-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Bug: no-missing-import
doesn't use the require algorithm when TS has CJS moduleResolution
?
#349
Comments
Can you elaborate a bit on the "TS barrel files" and maybe point to some example or docs? There's barely any info in this issue, making it harder than it needs to be to act upon 🙏 |
Right, that concept I’m aware of, not sure that’s the common name for it though, never really heard that term before. Is this limited to TS or goes for all files that does that kind of thing? And what error are you getting? From the rule description:
But “should find imports from TS barrel files” sounds like it’s not about paths in your case but about what’s imported? Path validation shouldn’t care if it’s a “barrel file” or non-barrel one |
Following throws errors for me:
where |
Right, then the issue is probably this: You use TS-syntax that compiles to CommonJS syntax, since only CommonJS ( ESM imports are required to include the full path, including file extensions. Have you specified your tsconfig? https://github.com/eslint-community/eslint-plugin-n/blob/master/docs/rules/no-missing-import.md#tsconfigpath |
No, I am not using CommonJS anywhere in the application. I am using ESM everywhere and everything transpiles just fine. |
Can you break this example using a barrel? (Just paste the new url here once you have it not working) |
So it's exactly what @voxpelli said:
You will need to specify the full path to the file path (including file extension) So, this will error: And this will not: Edit: There is also no way to change this currently: |
Ok, if TS code with barrel imports is not supported and there are no plans to introduce the support, feel free to close this as "won't fix". |
It should be doable, the basics is just adding The other option would be an option called A final option would be to expose the resolver options found here: This is also useful if |
|
The functionality is called Using folders as modules in the CJS docs of Node.js, and is considered legacy with subpath imports being the recommended replacement. The requirement to specify I think rather than |
|
Have you investigated why / how it does that? Does it still support it if you set these in your "module": "nodenext",
"moduleResolution": "nodenext", |
Yes, it works fine with module resolution set to {
"compilerOptions": {
/* Base Options: */
"esModuleInterop": true,
"skipLibCheck": true,
"allowJs": true,
"resolveJsonModule": true,
"isolatedModules": true,
"strictNullChecks": true,
"strictPropertyInitialization": false,
"forceConsistentCasingInFileNames": true,
"allowSyntheticDefaultImports": true,
/* Strictness */
"strict": true,
/* Decorators */
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2021",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "dist",
"types": [
"./typings.d.ts"
]
},
"include": [
"./srv"
]
} |
According to
Relatedly: I did an investigation related to this now, as they have done some work on enabling ESM-files to be loaded through I did still ask the author of that feature about it on Twitter: https://twitter.com/voxpelli/status/1841818608713826693 |
My code is ESM TS but for production I am transpiling to CJS. So, the output is CJS. |
It matters because different module resolution is applied when a javascript file is loaded through Sounds to me like If you switch to |
Yes, I get exactly the same errors.
Sorry, but I have to strongly disagree. The annoying one here is |
Then that could be a bug. @scagood When a TS-file is configured to have CJS module resolution, does
Node.js supports importing CJS files in ESM files by using The issue here though isn't whether The issue is that TypeScript in some cases apply the Only thing that remains to be figured out is why |
no-missing-import
doesn't work with TS barrel files (index.ts
)no-missing-import
doesn't use the require algorithm when TS has CJS moduleResolution
?
But you still can't use |
You can use import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
// sibling-module.js is a CommonJS module.
const siblingModule = require('./sibling-module');
Those currently are only needed for the type that differs from the As mentioned, starting in Node 23 – and already by using That said, lets focus on supporting the |
@hakimio You could try |
What's the difference between those two? |
Currently, the rule does not distinguish between common and module resolution, it just does esm resolution on
|
That would be nice, for this specific case its more a matter of checking |
Considering that there was effort put to improve TS support (#139) that would be a big step backwards. Especially when the fix for the issue is quite simple:
|
That's an incorrect fix. The correct fix is to implement the correct algorithm informed by what TS considers to be the correct algorithm. That involves a few things:
|
That might be way beyond this rule's scope... |
Exactly my feeling, especially as |
Environment
Node version: v20.13.1
npm version: v10.8.1
ESLint version: v9.11.1
eslint-plugin-n version: v17.10.3
Operating System: Windows 10
What rule do you want to report?
no-missing-import
What did you expect to happen?
The plugin should find imports from TS barrel files.
Participation
Additional comments
No response
The text was updated successfully, but these errors were encountered: