nuxt/eslint

Error [ERR_REQUIRE_ESM] when using flat config in ESLint w/ CJS

brownsugar opened this issue · 7 comments

Environment


  • Operating System: Linux
  • Node Version: v20.9.0
  • Nuxt Version: -
  • CLI Version: 3.11.1
  • Nitro Version: -
  • Package Manager: pnpm@8.10.2
  • Builder: -
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Package

@nuxt/eslint-config

Reproduction

https://codesandbox.io/p/devbox/nuxt-eslint-xxmhxc

Describe the bug

When using @nuxt/eslint-config package in ESLint with CJS config will throw an error.

Additional context

# eslint.config.cjs
const { createConfigForNuxt } = require("@nuxt/eslint-config/flat");
module.exports = createConfigForNuxt();

Logs

Error [ERR_REQUIRE_ESM]: require() of ES Module /workspaces/workspace/node_modules/.pnpm/find-up@7.0.0/node_modules/find-up/index.js from /workspaces/workspace/node_modules/.pnpm/eslint-config-flat-gitignore@0.1.5/node_modules/eslint-config-flat-gitignore/dist/index.cjs not supported.

Any reason to use eslint.config.cjs instead of eslint.config.mjs?

Any reason to use eslint.config.cjs instead of eslint.config.mjs?

When I use eslint.config.mjs I receive this error:

Cannot write file '/path/to/project/eslint.config.mjs' because it would overwrite input file.ts

@vctrtvfrrr, This is not relevant. Set noEmit: true in your tsconfig.json

The error mentioned (Cannot write file...) appears in the server/tsconfig.json file of any Nuxt project when using the new @nuxt/eslint module along with the flat config file.

Setting noEmit: true works, but it would be nice if the module handled this internally.
Can the module add this setting to .nuxt/tsconfig.server.json?

Maybe by adding the following hook?

nuxt.hook('nitro:config', (config) => {
  config.typescript.tsConfig.compilerOptions.noEmit = true;
});

@antfu I was trying to make a minimal shared config without using bundlers so just wrote it in CJS format.

We would suggest always use eslint.config.mjs as there is almost no reason to not to (it works even in a CJS project). If you absolutely need CJS, I'd suggest you to use dynamic import then.