Parser Options and ECMAScript Versions
You'll learn how to tell ESLint which version of JavaScript you're using so it can lint your code correctly.
When ESLint reads your code, it needs to know which flavor of JavaScript you're writing. Are you using ES5, ES6 (ES2015), or the latest ES2024 features? Are you writing CommonJS modules or ES modules? The parserOptions setting in your ESLint configuration file tells the linter exactly what to expect.
Why This Matters
Think of parserOptions as setting the "reading glasses" ESLint uses to look at your code. If you're using modern JavaScript features like async/await or optional chaining (?.), but ESLint thinks you're writing ES5, it'll throw errors on perfectly valid code. Conversely, if you tell ESLint you're using modern JavaScript, it can warn you when you accidentally use features that won't work in your target environment.
Key Parser Options
Here's what you typically configure in your .eslintrc file:
{
"parserOptions": {
"ecmaVersion": 2022,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
}
}
ecmaVersion: The JavaScript version (3, 5, 2015-2023, or "latest")sourceType: Either"script"(traditional) or"module"(uses import/export)ecmaFeatures: Enables special syntax like JSX
Setting ecmaVersion: "latest" tells ESLint to always use the newest supported JavaScript features, while a specific year locks it to that version's syntax.
Key Takeaway: Configure parserOptions to match the JavaScript version and module system you're actually using, ensuring ESLint understands your code correctly and provides accurate feedback.