Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Versions

Configure Language Options

Tip

The JavaScript ecosystem has a variety of runtimes, versions, extensions, and frameworks. Each of these can have different supported syntax and global variables. ESLint lets you configure language options specific to the JavaScript used in your project, like custom global variables. You can also use plugins to extend ESLint to support your project’s language options.

Specifying JavaScript Options

ESLint allows you to specify the JavaScript language options you want to support. By default, ESLint expects the most recent stage 4 ECMAScript syntax and ECMAScript modules (ESM) mode. You can override these settings by using thelanguageOptions key and specifying one or more of these properties:

  • ecmaVersion (default:"latest") - Indicates the ECMAScript version of the code being linted, determining both the syntax and the available global variables. Set to3 or5 for ECMAScript 3 and 5, respectively. Otherwise, you can use any year between2015 to present. In most cases, we recommend using the default of"latest" to ensure you’re always using the most recent ECMAScript version.
  • sourceType (default:"module") - Indicates the mode of the JavaScript file being used. Possible values are:
    • module - ESM module (invalid whenecmaVersion is3 or5). Your code has a module scope and is run in strict mode.
    • commonjs - CommonJS module (useful if your code usesrequire()). Your code has a top-level function scope and runs in non-strict mode.
    • script - non-module. Your code has a shared global scope and runs in non-strict mode.

Here’s an exampleconfiguration file you might use when linting ECMAScript 5 code:

// eslint.config.jsimport{ defineConfig}from"eslint/config";exportdefaultdefineConfig([{languageOptions:{ecmaVersion:5,sourceType:"script",},},]);

Specifying Parser Options

If you are using the built-in ESLint parser, you can additionally change how ESLint interprets your code by specifying thelanguageOptions.parserOptions key. All options arefalse by default:

  • allowReserved - allow the use of reserved words as identifiers (ifecmaVersion is3).
  • ecmaFeatures - an object indicating which additional language features you’d like to use:
    • globalReturn - allowreturn statements in the global scope.
    • impliedStrict - enable globalstrict mode (ifecmaVersion is5 or greater).
    • jsx - enableJSX.

Here’s an exampleconfiguration file that enables JSX parsing in the default parser:

// eslint.config.jsimport{ defineConfig}from"eslint/config";exportdefaultdefineConfig([{languageOptions:{parserOptions:{ecmaFeatures:{jsx:true,},},},},]);
Important

Specifying Globals

Some of ESLint’s core rules rely on knowledge of the global variables available to your code at runtime. Since these can vary greatly between different environments as well as be modified at runtime, ESLint makes no assumptions about what global variables exist in your execution environment. If you would like to use rules that require knowledge of what global variables are available, you can define global variables in your configuration file or by using configuration comments in your source code.

Using configuration comments

To specify globals using a comment inside of your JavaScript file, use the following format:

/* global var1, var2 */

This defines two global variables,var1 andvar2. If you want to optionally specify that these global variables can be written to (rather than only being read), then you can set each with a"writable" flag:

/* global var1:writable, var2:writable */

Using configuration files

To configure global variables inside of aconfiguration file, set thelanguageOptions.globals configuration property to an object containing keys named for each of the global variables you want to use. For each global variable key, set the corresponding value equal to"writable" to allow the variable to be overwritten or"readonly" to disallow overwriting. For example:

// eslint.config.jsimport{ defineConfig}from"eslint/config";exportdefaultdefineConfig([{languageOptions:{globals:{var1:"writable",var2:"readonly",},},},]);

This configuration allowsvar1 to be overwritten in your code, but disallow it forvar2.

Globals can be disabled by setting their value to"off". For example, in an environment where most globals are available butPromise is unavailable, you might use this config:

// eslint.config.jsimport{ defineConfig}from"eslint/config";exportdefaultdefineConfig([{languageOptions:{globals:{Promise:"off",},},},]);
Tip

For historical reasons, the boolean valuefalse and the string value"readable" are equivalent to"readonly". Similarly, the boolean valuetrue and the string value"writeable" are equivalent to"writable". However, the use of these older values is deprecated.

Predefined global variables

Apart from the ECMAScript standard built-in globals, which are automatically enabled based on the configuredlanguageOptions.ecmaVersion, ESLint doesn’t provide predefined sets of global variables. You can use theglobals package to additionally enable all globals for a specific environment. For example, here is how you can addconsole, amongst other browser globals, into your configuration.

// eslint.config.jsimport globalsfrom"globals";import{ defineConfig}from"eslint/config";exportdefaultdefineConfig([{languageOptions:{globals:{...globals.browser,},},},]);

You can include multiple different collections of globals in the same way. The following example includes globals both for web browsers and forJest:

// eslint.config.jsimport globalsfrom"globals";import{ defineConfig}from"eslint/config";exportdefaultdefineConfig([{languageOptions:{globals:{...globals.browser,...globals.jest,},},},]);
Edit this page
© OpenJS Foundation and ESLint contributors,www.openjsf.org. Content licensed underMIT License.
Change Language

[8]ページ先頭

©2009-2025 Movatter.jp