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

Rule to ensure explicit paths as required by ES Modules #2319

Open
gustavopch opened this issue Dec 4, 2021 · 5 comments
Open

Rule to ensure explicit paths as required by ES Modules #2319

gustavopch opened this issue Dec 4, 2021 · 5 comments

Comments

@gustavopch
Copy link

I've tried to find some way to ensure explicit paths as required by ES Modules, but couldn't find anything. To be clear, I want a rule that forces the code to use explicit extensions and indexes.

import x from './some-file' // ❌ Implicit extension
import x from './some-directory' // ❌ Implicit index

import x from './some-file.js' // ✅ Explicit extension
import x from './some-directory/index.js' // ✅ Explicit index

Is there a rule already for that?

@Haroenv
Copy link

Haroenv commented Dec 7, 2021

this would be import/extensions if I remember it correctly, but note that it doesn't work yet with typescript #2011, #2111, #2170 (not sure yet what the solution is)

@ljharb
Copy link
Member

ljharb commented Dec 7, 2021

Handling this properly would likely require “exports” support, and native ESM support, which we (and much of the ecosystem) don’t have yet. This plugin still assumes you’re not using native ESM in node, and are transpiling with Babel or typescript.

@wojtekmaj
Copy link

With ESM support maturing in Node.js I think this issue is worth revisiting.

@ljharb
Copy link
Member

ljharb commented Jan 5, 2023

@wojtekmaj once resolve supports "exports", and then ESM, then this plugin will, and it will be revisited.

However, it remains the case that ESM-only packages fail to get adoption because they aren't usable in CJS projects, and CJS-only (or dual) packages are the easiest to use and adopt, so the ecosystem has pretty clearly demonstrated it's a low priority.

@satazor
Copy link

satazor commented Jan 15, 2023

I wanted the exact same feature, so I created a small plugin: https://github.com/satazor/eslint-plugin-import-esm. Hope it helps until this plugin gets the feature requested in this issue.

Please note however, it doesn’t support exports in the package.json.

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

No branches or pull requests

5 participants