Bigint literals are not available when targeting lower than es2020.

The error message “bigint literals are not available when targeting lower than es2020” usually occurs when trying to use bigint literals in JavaScript code that is being compiled or transpiled to a version of ECMAScript (ES) lower than ES2020.

BigInt literals are a feature introduced in ECMAScript 2020, which allow representing integers larger than the maximum safe integer size (2^53 – 1) as BigInt values. BigInt literals use the `n` suffix, for example:

    const bigIntNumber = 1234567890123456789012345678901234567890n;
  

To use bigint literals, you need to ensure that your JavaScript code is targeting ES2020 or a newer version. However, if you are targeting an older version of ES, you will encounter the mentioned error because the language specification for that version does not recognize bigint literals.

To overcome this issue, you can transpile your code using a tool like Babel. Babel is a popular JavaScript compiler that can convert modern JavaScript syntax and features to an older target version. By configuring Babel to target a specific ES version, it will automatically transform bigint literals into equivalent code that can be understood by that target version. Here’s an example Babel configuration:

    {
      "presets": [
        [
          "@babel/preset-env",
          {
            "targets": {
              "esmodules": true,
              "chrome": "58",
              "firefox": "54",
              "ie": "11"
            }
          }
        ]
      ]
    }
  

In the above configuration, Babel is set to target ES2015 (ES6) or newer versions. By specifying the target browsers’ versions, Babel will transpile the code to ensure compatibility with those browsers.

Once your code is transpiled, you can safely use bigint literals even when targeting older versions of ES. The transpiled code will use alternative representations, such as using the BigInt function or converting strings to bigint values, to achieve the same result.

Similar post

Leave a comment