question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: Require extension for relative imports and re-exports

See original GitHub issue

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 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:open
  • Created 2 years ago
  • Reactions:9
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
silverwindcommented, Sep 23, 2021

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.

1reaction
fiskercommented, Apr 20, 2021

Fail

import foo from './foo?query=foo.js';

Pass

import foo from './foo.mjs';
import foo from './foo.json' assert { type: "json" };

(This can’t test yet, as @babel/eslint-parser missing assertions)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found