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.

`ERR_REQUIRE_ESM` when requiring `.eslintrc.js`

See original GitHub issue

Tell us about your environment

macOS, node v12.11.0

  • ESLint Version: 6.4.0
  • Node Version: 12.11.0
  • npm Version: 6.11.3

What parser (default, Babel-ESLint, etc.) are you using? default

Please show your full configuration:

Configuration
module.exports = {
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": "eslint:recommended",
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parserOptions": {
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    "rules": {
    }
};

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

I’ve set "type": "module" in the package.json of my project and run eslint

What did you expect to happen?

No internal errors in eslint

What actually happened? Please include the actual, raw output from ESLint.

> eslint lib/**/*.js test/**/*.js

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/findutnyy/Code/indutny/peerlinks/.eslintrc.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:958:13)
    at Module.load (internal/modules/cjs/loader.js:798:32)
    at Function.Module._load (internal/modules/cjs/loader.js:711:12)
    at Module.require (internal/modules/cjs/loader.js:838:19)
    at require (/Users/findutnyy/Code/indutny/peerlinks/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
    at module.exports (/Users/findutnyy/Code/indutny/peerlinks/node_modules/import-fresh/index.js:28:9)
    at loadJSConfigFile (/Users/findutnyy/Code/indutny/peerlinks/node_modules/eslint/lib/cli-engine/config-array-factory.js:189:16)
    at loadConfigFile (/Users/findutnyy/Code/indutny/peerlinks/node_modules/eslint/lib/cli-engine/config-array-factory.js:251:20)
    at ConfigArrayFactory._loadConfigDataInDirectory (/Users/findutnyy/Code/indutny/peerlinks/node_modules/eslint/lib/cli-engine/config-array-factory.js:436:34)
    at ConfigArrayFactory.loadInDirectory (/Users/findutnyy/Code/indutny/peerlinks/node_modules/eslint/lib/cli-engine/config-array-factory.js:401:18)

Are you willing to submit a pull request to fix this bug?

Maybe… although, it would require time to investigate this and learn the codebase first.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
platinumazurecommented, Oct 15, 2019

Not speaking for the team here, this is just my own 2 cents.

When considering supported file types, module types, runtimes, etc., I think ESLint needs to consider those at two different levels:

  • Code being linted/processed
  • Configuration

ESLint has a philosophy of trying not to favor one runtime over others. I understand that philosophy as applying to “Code being linted/processed”. We have features like env and shareable config to make files from certain environments easier to lint, but ESLint is fundamentally agnostic of the runtime environment as much as possible.

On the other hand, ESLint itself runs on Node.js. That is a specific decision we made-- we don’t run(*) on browsers, RhinoJS, or other runtimes. We are a Node package.

So, for an issue like this that is about the execution of ESLint from Node, I think we need to follow Node.js’s lead as much as we reasonably can and try to support different Node module styles, within reason. (This is not the same as saying we need to make ESLint async so it can be imported as an ES module itself.)

So based on that, if .cjs is official/not experimental (or will be very soon), I think we need to support it by allowing .eslintrc.cjs (and have it work the same as our current .js config implementation).


(*) Okay, we have webpack/browserify which creates a browser implementation, but that is not officially supported.

4reactions
guybedfordcommented, Sep 26, 2019

The ideal theoretical fix would be for your eslint file to be loaded as an ES module itself since the package boundary is supposed to have all .js files as ES modules by adding the type. Alternatively support could be added for eslint.cjs which is permitted inside of ESM package boundaries.

That is,

  1. Use import() first if available to load configs, OR
  2. Support eslint.cjs
Read more comments on GitHub >

github_iconTop Results From Across the Web

ESLint - Error: Must use import to load ES Module
The problem can be easily resolved by changing the file extension of .eslintrc from js to json (exporting the .eslintrc object using ESM ......
Read more >
Error [ERR_REQUIRE_ESM]: require() of ES Module not ...
The error [ERR_REQUIRE_ESM]: require() of ES Module not supported. Instead change the require of index.js to a dynamic import() which is available in...
Read more >
Must use import to load ES Module - Netlify Support Forums
Hello, I have been getting this error on production, but it works fine in my local when I try to preview using netlify...
Read more >
error require esm - You.com | The AI Search Engine You Control
js to a dynamic import() which is available in all CommonJS modules. As I understand, file-type package using only ESM import, but ,...
Read more >
ERR_REQUIRE_ESM - DEV Community ‍ ‍
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ~/projects/semantic-release-toolkit/node_modules/zz/cjs.js require() of ES ...
Read more >

github_iconTop Related Medium Post

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