Is it possible to deactivate this error in eslint?
Parsing error: 'import' and 'export' may only appear at the top level Is it possible to deactivate this error in eslint?
Parsing error: 'import' and 'export' may only appear at the top level ESLint natively doesnt support this because this is against the spec. But if you use babel-eslint parser then inside your eslint config file you can do this:
{ "parser": "babel-eslint", "parserOptions": { "sourceType": "module", "allowImportExportEverywhere": true } } Doc ref: https://github.com/babel/babel-eslint#configuration
Support for dynamic import has been added in eslint 6.2.
You need to set ecmaVersion to 11 (or 2020) though.
"parserOptions": { "ecmaVersion": 11 ... } You can test that in their online demo.
sourceType: scriptadd this to your eslint file:
env: { browser: true, commonjs: true, module: true, es2021: true, node: true, }, Had similar issue with eslint v9.x.x. Test files were modules (.mjs) and files being tested commonJs (js). Fixed it with an eslint config change.
Error
Running npx eslint in root of project returns this error:
/home/xxx/projects/yyy/src/browser/js/main.mocha.test.mjs 1:1 error Parsing error: 'import' and 'export' may appear only with 'sourceType: module' Original eslint.config.mjs
One piece of config applies to both file types js and mjs:
import globals from "globals"; import pluginJs from "@eslint/js"; export default [ { ...pluginJs.configs.recommended, name: "browser code", files: ["src/browser/**/*.{js,mjs}"], languageOptions: { sourceType: "script", globals: {...globals.browser} } } ]; Updated eslint.config.mjs
The initial config has been duplicated and the file types amended so the former is applied to commonJs files and the latter to module files. In the module config the sourceType is amended to module:
import globals from "globals"; import pluginJs from "@eslint/js"; export default [ { ...pluginJs.configs.recommended, name: "browser code", files: ["src/browser/**/*.js"], languageOptions: { sourceType: "script", globals: {...globals.browser} } }, { ...pluginJs.configs.recommended, name: "browser code module", files: ["src/browser/**/*.mjs"], languageOptions: { sourceType: "module", globals: {...globals.browser} } } ]; After these changes the error no longer flagged.
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 until 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, ];