Javascript

ESLint error Parsing error The keyword const is reserved

27 September 2026 · 5 min read

ESLint error Parsing error The keyword const is reserved

Encountering the dreaded “Parsing error: The keyword ‘const’ is reserved” in ESLint can be a frustrating roadblock for JavaScript developers. This error typically arises when you’re using more modern JavaScript syntax (like const and let) in an environment that doesn’t yet support it. Understanding the root cause and implementing the right solutions can save you valuable time and get your code running smoothly. This guide will walk you through the common causes of this error, provide practical solutions, and offer best practices to avoid it in the future.

Understanding the ‘const’ Keyword and its Compatibility

The const keyword, introduced in ECMAScript 6 (ES6), allows you to declare variables with values that cannot be reassigned. This is crucial for creating constants and improving code predictability. However, older JavaScript environments (like older browsers or certain server-side configurations) don’t inherently support ES6 features.

When ESLint encounters const in an environment that doesn’t recognize it, the parser throws the “Parsing error: The keyword ‘const’ is reserved” error. This signals a compatibility issue between your code and the target environment. This often occurs when your ESLint configuration isn’t aligned with the JavaScript version you’re using.

Key factors influencing compatibility include your browser version, Node.js version (if applicable), and build process (e.g., using Babel or Webpack).

Configuring ESLint for ES6 Support

The most common fix for the “const” keyword error involves configuring ESLint to understand your JavaScript version. This requires updating your ESLint configuration file (typically .eslintrc.js or .eslintrc.json). Here’s how you can specify the correct JavaScript environment:

  1. Specify the ECMAScript version: Within your ESLint configuration, add or modify the parserOptions property. Set the ecmaVersion to the appropriate version. For ES6 support, set it to 6 or higher. For example: ``` // .eslintrc.js module.exports = { parserOptions: { ecmaVersion: 6, // or higher, e.g., 2015, 2017, etc. sourceType: ‘module’, // if using modules }, };
  2. Define the environment: If you’re working in a specific environment like a browser or Node.js, specify it within the env property: ``` // .eslintrc.js module.exports = { env: { browser: true, // for browser environments node: true, // for Node.js environments }, };

Using Babel with ESLint for Broader Compatibility

If you need to support older environments that don’t natively understand ES6, Babel is an excellent tool. Babel transpiles your modern JavaScript code into code that older environments can execute.

To integrate Babel with ESLint, you’ll need to install the babel-eslint parser and configure ESLint to use it:

npm install babel-eslint --save-dev 

Then, update your ESLint configuration:

// .eslintrc.js module.exports = { parser: 'babel-eslint', // ... other configurations }; 

This setup allows ESLint to parse your code through Babel, effectively resolving the const keyword error even when targeting older environments.

Troubleshooting Common ESLint and Babel Integration Issues

Sometimes, even with the correct configurations, integration issues can arise. Here’s a checklist for troubleshooting:

  • Verify Package Versions: Ensure your eslint, babel-eslint, and other related packages are up-to-date. Incompatibilities between versions can sometimes lead to unexpected errors.
  • Check Babel Configuration: Confirm that your Babel configuration (usually in a .babelrc or babel.config.js file) is correctly set up to handle ES6 features. Ensure you have the necessary presets and plugins installed and configured, such as @babel/preset-env.

For more in-depth troubleshooting, consult the official ESLint and Babel documentation. These resources provide detailed information on configuration options, common issues, and advanced setups.

Best Practices for Avoiding JavaScript Compatibility Errors

Here are a few best practices to minimize JavaScript compatibility issues and ensure smoother development:

  • Stay Updated: Keep your development tools (Node.js, npm, ESLint, Babel) and browser versions up-to-date to leverage the latest language features and bug fixes.
  • Use a Build Process: Employing a build process with Babel ensures your code is transpiled into a format compatible with your target environments. This is essential for supporting a wider range of users.
  • Consistent ESLint Configuration: Maintain a consistent ESLint configuration across your projects to prevent discrepancies and unexpected errors. Shareable configurations can help enforce coding standards and best practices within your team.

By understanding the cause of the “Parsing error: The keyword ‘const’ is reserved,” configuring ESLint correctly, and following best practices, you can effectively resolve this issue and write clean, modern JavaScript code. Remember to keep your tools updated and leverage the power of Babel for enhanced compatibility across different environments. Check out this helpful resource on JavaScript best practices: MDN JavaScript Guide. For a deeper dive into ESLint, visit the official ESLint documentation. For Babel specific guidance see their official Babel documentation.

Still facing challenges? Consult the Troubleshooting Guide for more solutions.

FAQ: Common Questions about ESLint and ‘const’ Errors

Q: Why am I getting this error even after updating my ESLint configuration?

A: Double-check that you’ve restarted your development server or editor after making changes to the ESLint configuration. Sometimes the changes don’t take effect immediately.

Q: Can I use const without Babel?

A: Yes, if you’re only targeting modern browsers or environments that natively support ES6, you may not need Babel. However, for broader compatibility, Babel is recommended.

Q: Are there alternatives to using const?

A: In older JavaScript environments, you can use var to declare variables. However, const (and let) offer better scoping and prevent accidental reassignments, leading to more predictable and maintainable code. Therefore, using const (along with Babel if necessary) is the recommended approach for modern JavaScript development.

Question & Answer :
I am getting this error from ESLint:

error Parsing error: The keyword 'const' is reserved 

from this code:

const express = require('express'); const app = express(); const _ = require('underscore'); 

I’ve tried removing node_modules and reinstalling all npm packages (as suggested here), but to no avail.

ESLint defaults to ES5 syntax-checking. You’ll want to override to the latest well-supported version of JavaScript.

Try adding a .eslintrc.json file to your project. Inside it:

{ "env": { "es6": true } } 

See also this example .eslintrc.json which might help.