👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Error with top-level await while running data migration script

1 replies
Last updated: May 26, 2023
Hello! I am trying to write some data migration scripts and started with something basic and am running into an error that, while it is not really a Sanity issue, it is while I’m running through
sanity exec
and was not sure if that had anything to do with it. I’m running my command as such from the root of my Sanity project
sanity exec ./scripts/migration/migrateBlips.ts --with-user-token
and my code which is pretty straight forward gives me the error
Top-level await is currently not supported with the "cjs" output format
Here is my code

import { getCliClient } from 'sanity/cli';


const client = getCliClient().withConfig({ apiVersion: '2023-05-24' })

const loadCategories = async () => {
    const categories = await client.fetch('*[_type == "category"]');
    categories.forEach(category => {
        console.log(category)
    });
}

try {
  await loadCategories();
} catch (error) {
  console.error(error);
}
Thanks for any pointers…..
May 26, 2023, 5:15 PM
Looks like wrapping in an IIFE helps

(async () => {
  await loadCategories();
})();
May 26, 2023, 5:29 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?