⚠ Error - Top-level await is currently not supported with the "cjs" output format
I am trying to migrate to mysql database. I have already defined the schema and generated the migration files. But I try to migrate, I am getting this weird error -
11 Replies
Migrate.ts -
db.ts -
drizzle config -
I additionally added these -
tsconfig -
package.json -
I think your ts config spits out cjs, that is why
in common js module system you can't have await keyword in global context
You see your migration file uses the await in global context
possible fix wrap in Immediately Invoked Function Expressions (IIFE) in
same here
so you either change the ts config or wrap in IIFE
How do I change the ts config?
I mean what should I add or remove?
IIFE didnt work
Same error
There should a target option
Try changing to es 2020
And btw gimme a screen of TS config
This is because your package.json needs to have “type”: “module” to be 100% esm. But depending on your project it can cause some issues if one of your dependencies is not esm.
The iife doesn’t work because your db.ts uses an await.
Mysql2 has a synchronous way too (https://github.com/rphlmr/drizzle-mysql-commonjs/blob/main/config.js)
GitHub
drizzle-mysql-commonjs/config.js at main · rphlmr/drizzle-mysql-com...
Contribute to rphlmr/drizzle-mysql-commonjs development by creating an account on GitHub.
You could also try to await import your db in the iief
Note: drizzle-kit has a migrate command too
This is my migrate script of one of my projects.
I create a temp client in the function
I added "type" : "module" and now I am getting this new error -
Looks good
yes now it's an other issue. You need to provide the path for migration folder relatives to your script execution.
ex in my script:
${__dirname}/migrations
but I think __dirname
is a cjs thing so it will not work.
2 solutions:
1: make it esm compatible
2: don't care (keep your package.json like it was) and just create a client in your migrate script, inside the function
3: use drizzle-kit migrate and no need for a scriptThanks a lot
I will try them and inform which one did the job :]