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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot load CJS custom rules with rulesdir when project is "type": "module" #13921

Closed
hsablonniere opened this issue Dec 11, 2020 · 2 comments
Closed
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion

Comments

@hsablonniere
Copy link

Hello ESLint team 馃槈

Env

  • Node version: v14.15.0
  • npm version: v6.14.8
  • Local ESLint version: v7.15.0 (Currently used)
  • Global ESLint version: Not found

The Context

  • I want to add "type": "module" to the package.json of my project
  • I renamed my config files to .eslintrc.cjs and it's almost working

The "problem"

  • I have a custom rule inside a directory
  • This custom rule is a CommonJS script with a .js extention eslint-rules/my-rule.js
  • I run this npm script: eslint --rulesdir eslint-rules src

I get this error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/hsablonniere/dev/clever-components/eslint-rules/sort-lit-element-css-declarations.js
require() of ES modules is not supported.
require() of /home/hsablonniere/dev/clever-components/eslint-rules/sort-lit-element-css-declarations.js from /home/hsablonniere/dev/clever-components/node_modules/eslint/lib/cli-engine/load-rules.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename sort-lit-element-css-declarations.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/hsablonniere/dev/clever-components/package.json.

The diagnostic

  • My project has "type": "module"
  • ESLint CLI uses require() for scripts in rulesdir (see here)

From what I understand from Node.js' docs and how ESM vs CJS works, with this setup:

  • the required file must be a CJS file
  • the required file must have a .cjs extension

It's already a CJS file but I cannot change the extension to .cjs because ESLint ignores files that aren't .js (see here).

The proposition

Could we maybe add .cjs as allowed extensions in this check?

It tried this locally and it fixes my problem:

fs.readdirSync(rulesDir).forEach(file => {
  if (path.extname(file) !== ".js" && path.extname(file) !== ".cjs") {
    return;
  }
  rules[path.parse(file).name] = require(path.join(rulesDir, file));
});

Questions

  1. What do you think about this problem?
  2. What do you think about my proposition?
  3. Do you want me to write a PR?

Cheers...

@hsablonniere hsablonniere added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Dec 11, 2020
@mdjermanovic mdjermanovic added core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon bug ESLint is working incorrectly labels Dec 11, 2020
@mdjermanovic
Copy link
Member

Hi @hsablonniere, thanks for the issue!

It looks like this use case wasn't covered by eslint/rfcs#43.

I think we could support this, but the --rulesdir feature will be anyway removed in some of the future ESLint versions (per eslint/rfcs#9), so I'm not sure should we be adding some enhancements to this feature now if there's at least some workaround for "type": "module" projects.

Can you add an empty package.json file to the directory where the custom rules are, and would that work for you? (extensions should be still .js).

@hsablonniere
Copy link
Author

Hello @mdjermanovic,

  • I just had a quick look at the RFC and the replacement for 藡鈥攔ulesdir藡 seems really nice.
  • I totally understand how my proposition is not relevant if this is going away
  • I created a 芦聽local聽禄 plugin with a 藡file:藡 dependency but your solution feels simpler and smarter thanks very much 馃憤

Thanks for your quick feedback and answer and best regards to the team.

@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Jun 10, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jun 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion
Projects
None yet
Development

No branches or pull requests

2 participants