Rule proposal: Require extension for relative imports and re-exports
See original GitHub issueIt would enforce using a .js
extension for relative imports if it doesn’t already have an extension.
We cannot auto-fix as we cannot know whether import foo from './foo';
refers to a file or directory.
This even applies to TypeScript, where you still need the .js
extension even if you import other .ts
files.
There’s already the import/extensions
rule, but that project is not very actively maintained, very buggy, and also it doesn’t let us use .js
extension for TypeScript files. I think it’s worth adding this simple rule here.
This rule will not do anything about require()
statements.
Fail
import foo from './foo';
export foo from './foo';
await import('./foo');
Pass
import foo from './foo.js';
export foo from './foo.js';
await import('./foo.js');
import foo from './foo.vue';
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Require extension for relative imports and re-exports #1194
It would enforce using a .js extension for relative imports if it doesn't already have an extension. We cannot auto-fix as we cannot...
Read more >Appending .js extension on relative import statements during ...
it resolves the import to a specific file, adding the js extension if there is none, since that is what the most popular...
Read more >Module Resolution - webpack
The relative path specified in the import/require is joined to this context path to produce the absolute path to the module.
Read more >Absolute vs Relative Imports in Python
Styling of Import Statements · Imports should always be written at the top of the file, after any module comments and docstrings. ·...
Read more >Content Types - ESBuild
The .cjs extension is used by node for CommonJS modules and the .mjs extension is used by ... If you need to access...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
An autofix for this rule would also be awesome.
import/extensions
doesn’t provide one, making it very tedious to manually refactor big projects from./file
to./file.js
imports. I guess such a autofix would require file system access to determine the correct extension, thought.Fail
Pass
(This can’t test yet, as @babel/eslint-parser missing assertions)