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.

"plugin:node/recommended" generates: Parsing error: 'import' and 'export' may appear only with 'sourceType: module'

See original GitHub issue

There was another issue like this, seen here: https://github.com/mysticatea/eslint-plugin-node/issues/135 but it was closed. But I felt it was warranted to open a new issue since this seems to be a problem with this plugin, which goes against the answer in that issue.

I have the following configuration:

  "extends": [
    "airbnb-base",
    "plugin:node/recommended",
    "plugin:jest/recommended",
    "plugin:jest/style",
    "plugin:jsdoc/recommended",
    "plugin:prettier/recommended"
  ],

If I remove "plugin:node/recommended",, the error Parsing error: 'import' and 'export' may appear only with 'sourceType: module' goes away. So it is this plugin generating this error somehow.

The reason why I get this is because I use commonjs (i.e. require()) instead of ecmascript modules (i.e. import) in my code. But for Jest test cases, I use import because those are transformed by babel anyway. So I get the error in those files only.

I can work around this by setting an overrides to use another sourceType like so:

  "overrides": [{
    "files": ["path/to/some/file.js", "path/to/some/folder/**.js"],
    "parserOptions": {
      "sourceType": "module"
    }
  }]

But the question is, why is this error generated at all due to the inclusion of "plugin:node/recommended",?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mysticateacommented, Jun 9, 2020

Your workaround (overrides parserOptions.sourceType) is right.

But it may be nice to use plugin:node/recommended-module instead.

  "overrides": [{
    "files": ["path/to/some/file.js", "path/to/some/folder/**.js"],
    "extends": "plugin:node/recommended-module"
  }]
  • It configures the rules of this plugin for ES modules. For example, it reports some global variables that we cannot use in ES modules, and tweaks the node/no-unsupported-features/es-syntax rule to ignore ES modules syntax.

Or, if you are sure that all *.js files are ES modules, you can use plugin:node/recommended-module instead of plugin:node/recommended:

  "extends": [
    "airbnb-base",
    "plugin:node/recommended-module",
    "plugin:jest/recommended",
    "plugin:jest/style",
    "plugin:jsdoc/recommended",
    "plugin:prettier/recommended"
  ],
0reactions
ljharbcommented, Dec 9, 2020

I’d suggest using “unflagged” and not “not experimental”, personally. It’s fully usable with no experimental warning in node v12.17.

Read more comments on GitHub >

github_iconTop Results From Across the Web

'import' and 'export' may appear only with 'sourceType: module ...
I get this error since the most recent update. is it possible there this is missing somewhere in the package?
Read more >
ignore eslint error: 'import' and 'export' may only appear at the ...
my solution incase other dont work "parserOptions": { "ecmaVersion": 6, "sourceType" ...
Read more >
Import and export declarations are not supported yet (in TS)
The problem When trying to use import or export statements in TypeScript (*.ts) ... //.eslintrc.js module.exports = { root: true, parser: ...
Read more >
'import' and 'export' may appear only with 'sourceType: module ...
Parsing error on .vue files during typescript analysis with javascript sensor - 'import' and 'export' may appear only with 'sourceType: ...
Read more >
Re: Using Require in V2? - Help - Pipedream
For some our modules we have to use require or else the module always ... we get the following error: 'import' and 'export'...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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