[BUG]: Top-level await is not available in the configured target environment ("node14")
What version of drizzle-orm are you using?
0.26.5
What version of drizzle-kit are you using?
0.18.1
Describe the Bug
drizzle.config.ts
import type { Config } from "drizzle-kit";
import { getDbUrl } from "./src";
export default {
schema: "./src/schema.ts",
out: "./.migrations",
connectionString: await getDbUrl(), // ERRORS HERE
} satisfies Config;
Error message
/home/pedro/Documents/soluciones-contables/packages/db/drizzle.config.ts:8:20: ERROR: Top-level await is not available in the configured target environment ("node14")
at failureErrorWithLog (/home/pedro/Documents/soluciones-contables/node_modules/drizzle-kit/node_modules/esbuild/lib/main.js:1575:15)
at /home/pedro/Documents/soluciones-contables/node_modules/drizzle-kit/node_modules/esbuild/lib/main.js:814:29
at responseCallbacks.<computed> (/home/pedro/Documents/soluciones-contables/node_modules/drizzle-kit/node_modules/esbuild/lib/main.js:680:9)
at handleIncomingPacket (/home/pedro/Documents/soluciones-contables/node_modules/drizzle-kit/node_modules/esbuild/lib/main.js:735:9)
at Socket.readFromStdout (/home/pedro/Documents/soluciones-contables/node_modules/drizzle-kit/node_modules/esbuild/lib/main.js:656:7)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at Pipe.onStreamRead (node:internal/stream_base_commons:190:23) {
errors: [
{
detail: undefined,
id: '',
location: {
column: 20,
file: '/home/pedro/Documents/soluciones-contables/packages/db/drizzle.config.ts',
length: 5,
line: 8,
lineText: ' connectionString: await getDbUrl(),',
namespace: '',
suggestion: ''
},
notes: [],
pluginName: '',
text: 'Top-level await is not available in the configured target environment ("node14")'
}
],
warnings: []
}
Node 14 support expired already btw
Expected behavior
I'm getting the database_url dynamically from aws secrets service, so I would like to use await for the fetch call to aws.
I don't know if I'm missing something here..
Environment & setup
No response
Just wrap it in async function and then call the function. Node does not support top-level await (unlike Deno).
I was a having a similar error but was able to solve it by adding this to my config in next.config.mjs:
webpack(config) {
config.experiments = { ...config.experiments, topLevelAwait: true };
return config;
},
My next.config.mjs now looks like this:
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially useful
* for Docker builds.
*/
await import("./src/env.mjs");
/** @type {import("next").NextConfig} */
const config = {
webpack(config) {
config.experiments = { ...config.experiments, topLevelAwait: true };
return config;
},
reactStrictMode: true,
images: {
domains: ["images.clerk.dev"],
},
/**
* If you have `experimental: { appDir: true }` set, then you must comment the below `i18n` config
* out.
*
* @see https://github.com/vercel/next.js/issues/41980
*/
i18n: {
locales: ["en"],
defaultLocale: "en",
},
typescript: {
ignoreBuildErrors: true,
},
eslint: {
ignoreDuringBuilds: true,
},
swcMinify: true,
};
export default config;
Hope this helps!
Just wrap it in async function and then call the function. Node does not support top-level await (unlike Deno).
import type { Config } from "drizzle-kit";
import { getDbUrl } from "./src";
async function conf() {
const dbUrl = await getDbUrl();
return {
schema: "./src/schema.ts",
out: "./.migrations",
connectionString: dbUrl,
} satisfies Config;
}
export default conf()
Now my index.ts errors:
import { drizzle } from "drizzle-orm/mysql2";
import { migrate } from "drizzle-orm/mysql2/migrator";
import mysql from "mysql2/promise";
import * as schema from "./schema";
export async function getDbUrl() {
return `mysql://username:password@host:port/dbname`;
}
const poolConnection = mysql.createPool({
uri: await getDbUrl(), // ERRORS HERE
});
export const db = drizzle(poolConnection, { schema });
await migrate(db, { migrationsFolder: "../.migrations/" }); // ERRORS HERE
Same error btw
Use Like this:
const db = drizzle(postgres(process.env.DB_URL, { max: 1 }));
// (async () => {
// try {
// await migrate(db, { migrationsFolder: "src/DataService/drizzle" });
// console.log("Migration Done");
// } catch (err) {
// console.error(err);
// }
// })();