nuxt / eslint

Collection of ESLint-related packages for Nuxt
https://eslint.nuxt.com
MIT License
539 stars 65 forks source link

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

Closed brownsugar closed 5 months ago

brownsugar commented 5 months ago

Environment


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.
antfu commented 5 months ago

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

vctrtvfrrr commented 5 months ago

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
antfu commented 5 months ago

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

mitjans commented 5 months ago

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?

mitjans commented 5 months ago

Maybe by adding the following hook?

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

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

antfu commented 5 months ago

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.