Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

How to enable Top level await in PayloadCMS project?

default discord avatar
harleyqueens2 years ago
7

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
    jmikrut
    2 years ago

    Hey

    @276227047968210945

    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';
    
    require('dotenv').config();
    const app = express();
    
    // Redirect root to Admin panel
    app.get('/', (_, res) => {
      res.redirect('/admin');
    });
    
    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.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
        },
      })
    
      // Add your own express routes here
    
      app.listen(3000);
    }
    
    start()


    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
    harleyqueens2 years ago

    Yes I am doing this rn



    But TOp level is cleaner

  • default discord avatar
    wiessonlast year

    I have the same question 🙂

  • default discord avatar
    remy_90last year

    Would love to hear what the plans are around this

  • default discord avatar
    wiessonlast year

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

  • default discord avatar
    runoncelast month

    Is this still the case? I was trying to follow the [example for creating a seeding script in Node](

    https://payloadcms.com/docs/local-api/outside-nextjs#importing-the-payload-config-outside-of-nextjs

    ) and got this error :



    Top-level await is currently not supported with the "cjs" output format

    Also tried

    bun payload run seed.ts

    but got same error

  • discord user avatar
    alessiogr
    last month

    This is supported, you just need to enable esm in your project. To do that, add "type": "module" to your package.json

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.