search
Javascript

Fix: Top-level await is not available in the configured target error

Quick fix for 'Top-level await is not available in the configured target' error. Learn how to enable top-level await in JavaScript environments.

person By Gautam Sharma
calendar_today January 8, 2026
schedule 2 min read
JavaScript Top-Level Await ES2022 Error Fix Compilation

The ‘Top-level await is not available in the configured target’ error occurs when using top-level await (await outside of async functions) in environments that don’t support it, typically due to outdated compilation targets or missing configuration.


How the Error Happens

❌ Error Scenario:

// ❌ This causes the error in unsupported environments
const data = await fetch('/api/data'); // ❌ Top-level await not supported
// Error: Top-level await is not available in the configured target

✅ Quick Fix - Enable Top-Level Await

Solution 1: Update TypeScript Configuration

// ✅ In tsconfig.json
{
  "compilerOptions": {
    "target": "ES2022", // ✅ ES2022+ supports top-level await
    "module": "ES2022",
    "moduleResolution": "node"
  }
}

Solution 2: Update Babel Configuration

// ✅ In babel.config.json
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "14.8" // ✅ Node 14.8+ supports top-level await
        }
      }
    ]
  ]
}

Solution 3: Wrap in Async Function

// ❌ Top-level await without support
// const data = await fetch('/api/data');

// ✅ Wrap in async IIFE (Immediately Invoked Function Expression)
const main = async () => {
  const data = await fetch('/api/data');
  return data.json();
};

main().catch(console.error);

Solution 4: Use ES Module in Browser

<!-- ✅ In HTML, use type="module" for top-level await support -->
<script type="module">
  const response = await fetch('/api/data');
  const data = await response.json();
  console.log(data);
</script>
Gautam Sharma

About Gautam Sharma

Full-stack developer and tech blogger sharing coding tutorials and best practices

Related Articles

Javascript

How to Resolve Dynamic import callback was not specified error

Quick fix for 'Dynamic import callback was not specified' error. Learn how to properly handle dynamic imports in JavaScript applications.

January 8, 2026
Javascript

Fix: ESM packages need to be imported error

Quick fix for 'ESM packages need to be imported' error. Learn how to properly import ES modules in Node.js and JavaScript environments.

January 10, 2026
Javascript

Fix: Error when starting dev server: port already in use error

Quick fix for 'Error when starting dev server: port already in use' error. Learn how to resolve port conflicts in JavaScript development servers.

January 8, 2026