Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing error: Unexpected token import #11189

Closed
seamlik opened this issue Dec 12, 2018 · 6 comments
Closed

Parsing error: Unexpected token import #11189

seamlik opened this issue Dec 12, 2018 · 6 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion

Comments

@seamlik
Copy link

seamlik commented Dec 12, 2018

  • ESLint Version: 5.10.0
  • Node Version: 11.4.0
  • npm Version: 6.4.1

What parser (default, Babel-ESLint, etc.) are you using?

Default.

Please show your full configuration:

Configuration
{
  "extends": "eslint",
  "parserOptions":{
    "ecmaVersion": "2018",
    "sourceType": "module"
  }
}

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

// Workaround for `--experimental-modules`
// See <https://nodejs.org/docs/latest/api/esm.html#esm_import_meta>
const __dirname = Path.dirname(new URL(import.meta.url).pathname)
eslint sample.mjs

What did you expect to happen?

A decent report.

What actually happened? Please include the actual, raw output from ESLint.

  3:40  error  Parsing error: Unexpected token import

✖ 1 problem (1 error, 0 warnings)

Are you willing to submit a pull request to fix this bug?

No.

@seamlik seamlik added bug ESLint is working incorrectly evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Dec 12, 2018
@platinumazure
Copy link
Member

Hi @seamlik, thanks for the issue.

I'm not sure I understand the problem. When using the default parser and sourceType: "module", the word import becomes a keyword token that can only be used in import declarations.

I quickly searched Google and it seems this might be using the import.meta proposal? If that's the case, it's currently in Stage 3 of the evaluation process, and ESLint's default parser only supports Stage 4 language features. So if your intention is to use that syntax, then you need to use a different parser such as babel-eslint.

@seamlik
Copy link
Author

seamlik commented Dec 12, 2018

Thanks for the quick reply.

Correct, import.meta is the currently the only way to use __dirname inside Node.js if you use --experimental-modules, as described in this ticket.

And so this would be a blocker for many users of ES modules in Node.js, I presume. (Or perhaps I'm the very few who is too experimental on things?)

@kaicataldo
Copy link
Member

@seamlik That's right. Currently, the solution is to use the babel-eslint parser. That being said, there are plans to change babel-eslint to expect babel as a peer dependency. The assumption is that anyone using babel-eslint is also using babel, but you've hit a case where that isn't true.

@seamlik
Copy link
Author

seamlik commented Dec 12, 2018

Now with this config:

{
  "extends": "standard",
  "parser": "babel-eslint"
}

and installing babel-eslint, the error was gone. Thanks for the solution!

Though I personally can't consider this ticket is resolved. But I'll leave the decision to you guys.

@ilyavolodin
Copy link
Member

ESLint policy is to support Stage 4 features. For anything below that, you would need to use Babel ESLint.
Closing this issue as resolved.

@Lonniebiz
Copy link

Lonniebiz commented Jan 24, 2019

When my configuration is like this:

'parserOptions':
{
	'ecmaVersion': 2018,
	'sourceType': 'module'
}

ESlint rejects "import" as it is used in this line of my code:

const __dirname = path.dirname(new URL(import.meta.url).pathname);

When I follow the configuration suggested by @seamlik :

'parserOptions':
{
	"extends": "standard",
	"parser": "babel-eslint"
}

I get this issue instead.

Here's a detailed summary of my issue.

Why not add another env profile to eslint, something like NodeESM?

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Jun 12, 2019
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Jun 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion
Projects
None yet
Development

No branches or pull requests

5 participants