How to enable Top level await in PayloadCMS project?

default discord avatar
10 months ago

When you initiate the payload cms project you get Target ES5, this does not support top level await.

I tried changing it to ES2022, ES2017, with module : ES2017 and moduleResolution : node and all sorts of combinations but the builds either fail or it crashes when you run it because of bad imports.

Has anybody here had any luck with changing the ts config to go beyond es5?

By the way the Use case is this:

Loading credentials from AWS SSM

  • discord user avatar
    Payload Team
    10 months ago

    Hey @harleyqueens I responded to your issue on GitHub, and I'd certainly be interested in tracking down any incompatible packages that Payload relies on to be able to run ESM

    to get around top-level await, for now, you could always just wrap your server code in a function and then call it

    import express from 'express';
    import payload from 'payload';
    const app = express();
    // Redirect root to Admin panel
    app.get('/', (_, res) => {
    const start = async () => {
      // Fetch your secrets
      await fetchSecrets()
      // Initialize Payload
      await payload.initAsync({
        secret: process.env.PAYLOAD_SECRET,
        mongoURL: process.env.MONGODB_URI,
        express: app,
        onInit: () => {
`Payload Admin URL: ${payload.getAdminURL()}`)
      // Add your own express routes here

    would something like that be a stop-gap solution for you?

    still though, it seems like we shouldn't need to support ESM to support a higher target than es5

    and even still, i'd expect that TS should be able to transpile top-level await to es5 anyway

    there's certainly more reading / thinking to be done here

  • default discord avatar
    10 months ago

    Yes I am doing this rn

    But TOp level is cleaner

  • default discord avatar
    last month

    I have the same question 🙂

  • default discord avatar
    last month

    Would love to hear what the plans are around this

  • default discord avatar
    last month

    I assume that once payload switches from CJS to ESM, everything will be better and we can have top level await

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!


Connect with the Payload Community on Discord



Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.