DT
Drizzle Teamβ€’13mo ago
Sturlen

Drizzle-kit can't load env file via t3-env

I'd like to use t3-env to validate my env variables in my Drizzle config file. I've tried importing t3-env in the config.ts file, but I'm hit with the error SyntaxError: Cannot use import statement outside a module in env.mjs, which I find odd since it is explicitly marked as an ES module. Other than this, t3-env works fine with drizzle-orm. Full Error:
Reading config file 'F:\Projects\PROJECTNAME\drizzle.config.ts'
F:\Projects\PROJECTNAME\src\env.mjs:2
import { createEnv } from "@t3-oss/env-nextjs";
^^^^^^

SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:360:18)
at wrapSafe (node:internal/modules/cjs/loader:1048:15)
at Module._compile (node:internal/modules/cjs/loader:1083:27)
at Module._compile (F:\Projects\PROJECTNAME\node_modules\.pnpm\drizzle-kit@0.18.1\node_modules\drizzle-kit\index.js:16334:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
at Object.newLoader [as .mjs] (F:\Projects\PROJECTNAME\node_modules\.pnpm\drizzle-kit@0.18.1\node_modules\drizzle-kit\index.js:16338:13)
at Module.load (node:internal/modules/cjs/loader:997:32)
at Module._load (node:internal/modules/cjs/loader:838:12)
at Module.require (node:internal/modules/cjs/loader:1021:19)
at require (node:internal/modules/cjs/helpers:103:18)

Node.js v18.9.0
Reading config file 'F:\Projects\PROJECTNAME\drizzle.config.ts'
F:\Projects\PROJECTNAME\src\env.mjs:2
import { createEnv } from "@t3-oss/env-nextjs";
^^^^^^

SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:360:18)
at wrapSafe (node:internal/modules/cjs/loader:1048:15)
at Module._compile (node:internal/modules/cjs/loader:1083:27)
at Module._compile (F:\Projects\PROJECTNAME\node_modules\.pnpm\drizzle-kit@0.18.1\node_modules\drizzle-kit\index.js:16334:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
at Object.newLoader [as .mjs] (F:\Projects\PROJECTNAME\node_modules\.pnpm\drizzle-kit@0.18.1\node_modules\drizzle-kit\index.js:16338:13)
at Module.load (node:internal/modules/cjs/loader:997:32)
at Module._load (node:internal/modules/cjs/loader:838:12)
at Module.require (node:internal/modules/cjs/loader:1021:19)
at require (node:internal/modules/cjs/helpers:103:18)

Node.js v18.9.0
This is my Drizzle config file:
import type { Config } from "drizzle-kit";
import { env } from "~/env.mjs";

export default {
schema: "./src/server/schema.ts",
out: "./drizzle",
connectionString: env.DATABASE_URL,
} satisfies Config;
import type { Config } from "drizzle-kit";
import { env } from "~/env.mjs";

export default {
schema: "./src/server/schema.ts",
out: "./drizzle",
connectionString: env.DATABASE_URL,
} satisfies Config;
I am I doing something wrong or is this just something drizzle-kit does not support atm?
12 Replies
D3.M4.R
D3.M4.Rβ€’13mo ago
Hi! Just a regular developer over here, did you manage to solve or workaround this issue?
dandadan
dandadanβ€’13mo ago
few questions: - whats your build/dev script - values of module and target inside tsconfig.json - value of type inside package.json (if set) my best guess atm is youre compiling the code into commonjs and the .mjs file was not included in the compilation easiest fix would probably be to just use typescript for ~/env.ts, main reason why they outsourced the package was to be able to use typescript instead of jsdoc in the env config file without messing around with too many options in the different config files (IIRC)
KIPRI πŸš€
KIPRI πŸš€β€’12mo ago
Hello. I'm having a similar problem, but, in my case, it seems that the env variables that I have set are not being read so I'm getting a "missing env variables" error from the createEnv func. Is there any way I can make drizzle-kit manually import env variables? Debugging this I can confirm that the env variables get loaded when building, npm run build . The only time they don't get read is when trying to push, generate, etc. using drizzle-kit.
Asseater Peter
Asseater Peterβ€’12mo ago
Yes ofcourse, you can manually import process.env using something like
import 'dotenv/config'
import 'dotenv/config'
KIPRI πŸš€
KIPRI πŸš€β€’12mo ago
Well yes, I’ve tried that and no dice.
Asseater Peter
Asseater Peterβ€’12mo ago
That is currently in use in an app for me, so the general concept works for sure. are your env variables stored in .env or are you using NextJS with .env.local?
KIPRI πŸš€
KIPRI πŸš€β€’12mo ago
Ofc I try it a second time and it’s working. 🫠 possible issue was I was not using dotenv at the correct step.
Asseater Peter
Asseater Peterβ€’12mo ago
Could be Β―\_(ツ)_/Β― Well, as long as it's working...
KIPRI πŸš€
KIPRI πŸš€β€’12mo ago
Thank you for helping!
Asseater Peter
Asseater Peterβ€’11mo ago
Anytime. btw, don't forget to mark the thread as done for my OCD's sake @sturlen
Sturlen
Sturlenβ€’11mo ago
the underlying problem is that drizzle-kit support for esm is not working yet, which was confirmed by Andrii Sherman in a another server (it did for a while, but they rolled it back. dotenv is the workaround I'm using for now as well, but the underlying problem is not solved answer source: https://www.answeroverflow.com/m/1117762215353790484
Answer Overflow
Is it possible to use t3env from a Drizzle config file? - Theo's Ty...
I'm using create-t3-app but I'd like to use Drizzle instead of Prisma. I've managed to get it working, but the one pain point I have is that I can't get t3env t...
Asseater Peter
Asseater Peterβ€’11mo ago
Oh absolutely. However, if you use @t3-oss/env-core you can import it into your drizzle.config.ts file without any issues Also, as drizzle-kit is only a dev tool anyways, I personally don't mind giving it its own settings in my NextJS app
Want results from more Discord servers?
Add your server
More Posts