At first, let me tell you one thing for the beginners of how can you find out your eslint config file.Here in this blog you will find out detailed explanation of how you can do it.Now come to the ans, most of the answers which has been posted here is very old.The config file format of eslint is ".eslintrc.json" this untill eslintv8.21.0 came in Aug 19,2022.Now eslint is using "eslint.config.mjs" this file configuration format.So, for this new file format we can't write code like what has been using for a long time in ".eslintrc.js" this file format. To solve this problem you just need to add one line of code and that is this one.
{ languageOptions: { sourceType: "module" } },
Here is the default "eslint.config.mjs" file code-
import globals from "globals";
import pluginJs from "@eslint/js";
import pluginReact from "eslint-plugin-react";
/** @type {import('eslint').Linter.Config[]} */
export default [
{ files: ["**/*.{js,mjs,cjs,jsx}"] },
{ files: ["**/*.js"], languageOptions: { sourceType: "commonjs" } },
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
pluginReact.configs.flat.recommended,
];
After adding languageoptions as module, the updated code looks like this-
import globals from "globals";
import pluginJs from "@eslint/js";
import pluginReact from "eslint-plugin-react";
/** @type {import('eslint').Linter.Config[]} */
export default [
{ files: ["**/*.{js,mjs,cjs,jsx}"] },
{ files: ["**/*.js"], languageOptions: { sourceType: "commonjs" } },
{ languageOptions: { sourceType: "module" } },
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
pluginReact.configs.flat.recommended,
];