Top-level await fails in sanity.config.ts build with esbuild target error

7 replies
Last updated: Jun 6, 2023
I’m using a top-level await to get the current user in my sanity.config.ts and it works fined in dev but throws “Top-level await is not available in the configured target environment ” when
npm run build
. I don’t know if there is a way to configure the targets that are used by esbuild. The top level await was actually recommended by a sanity member so it doesn’t seem like I’m doing something non-canon here.
AI Update

This is a known issue with Sanity Studio's build process! The error occurs because Vite (which Sanity Studio uses for building) has esbuild configured with a target that doesn't support top-level await by default during production builds, even though it works fine in development.

The solution is to configure Vite to use a newer build target that supports top-level await. You can do this in your sanity.cli.ts (or sanity.cli.js) file by extending the Vite configuration:

import { defineCliConfig } from 'sanity/cli';

export default defineCliConfig({
  api: {
    projectId: '<PROJECT_ID>',
    dataset: '<DATASET>',
  },
  vite: config => {
    return {
      ...config,
      build: {
        ...config.build,
        target: 'esnext',
      },
    };
  },
});

This tells Vite to target esnext during the build process, which includes support for top-level await. The key is that you're overriding the build.target setting while preserving the rest of Vite's default configuration.

As you mentioned, using top-level await in sanity.config.ts for things like fetching the current user is actually a valid pattern that Sanity team members have recommended, so you're not doing anything wrong! The issue is just that the default build target needs to be adjusted to support this modern JavaScript feature.

After making this change, your npm run build should work without throwing the "Top-level await is not available" error. This solution comes directly from a community answer on the same issue, where it successfully resolved the problem for other developers.

Show original thread
7 replies
have you tried using a third party lib like axios https://www.npmjs.com/package/axios
to what end?
well i find that using vanilla await with node has issues and this tool has a tendency to work when await does not.
This doesn’t make any sense, sorry. The top level await does work. It’s the build tool that is the problem as it’s disabled on a feature level
user B
What about this in your
sanity.cli.ts
file?

import { defineCliConfig } from 'sanity/cli';

export default defineCliConfig({
  api: {
    projectId: '<PROJECT_ID>',
    dataset: '<DATASET>',
  },
  vite: config => {
    return {
      ...config,
      build: {
        ...config.build,
        target: 'esnext',
      },
    };
  },
});
You’re the best! Works
Great! Glad to hear it. Full credit to a colleague of mine who I swiped it from. 🤫

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?