2

There are multiple questions about about import and require and their differences. Like these:

What is the difference between 'import' and 'require' in JavaScript?

The difference between "require(x)" and import x

ignore eslint error: 'import' and 'export' may only appear at the top level

I'm building an electron app with Vue and linting with ES6. But as soon as I use import I get a lint error:

[eslint] Parsing error: 'import' and 'export' may appear only with 'sourceType: module'

This happens both in the nodejs part of electron and the front end one.

The accepted answer in the last link says "ESLint natively doesnt support this because this is against the spec." and then gives an option of how to get rid of the error by using babel.

As I thought ES6 supports imports and I'm using ES6, why does it still complain about this?

I used vue-cli 3 to generate the project which has a main.js file which starts with import Vue from 'vue'; which gives me the lint error above even if running the code works.

Could someone clarify why ESLint set to use ES6 complains when using imports? And if its still not supported why does a framework like Vue use it? How would I properly use import with only ES6 without the use of babel?

just_user
  • 11,769
  • 19
  • 90
  • 135
  • https://github.com/AtomLinter/linter-eslint/issues/462 and https://eslint.org/docs/user-guide/configuring#specifying-parser-options – str Jan 24 '19 at 11:59
  • @str thanks, that takes away the error. But should sourceType always be considered a module? Both in backend like nodejs and frontend like javascript loaded from an html document? – just_user Jan 24 '19 at 12:05
  • 1
    It should be `module` if you use ES6 modules. So in Vue.js code probably yes, in Node.js (that uses CommonJS by default) probably not. – str Jan 24 '19 at 12:07
  • 1
    You must not confuse ES6 with ESModule syntax. Node.js suppport ES6 but doesn't support ESModule syntax (or [only experimental way](https://nodejs.org/docs/v11.7.0/api/esm.html) – Troopers Jan 24 '19 at 17:17

0 Answers0