`tsx ./src/server/db/SCRIPT_NAME` doesn't recognize environment variables.

I created a db migrator script similar to the recommendations here: https://create.t3.gg/en/usage/prisma/#seeding-your-database I'm trying to execute a script and the t3-env doesn't seem to load the .env file. src/server/db/migrator.ts is:
import { migrate } from "drizzle-orm/neon-http/migrator";
import { db } from "~/server/db/index";

await migrate(db, { migrationsFolder: "src/server/db/drizzle" });
import { migrate } from "drizzle-orm/neon-http/migrator";
import { db } from "~/server/db/index";

await migrate(db, { migrationsFolder: "src/server/db/drizzle" });
package.json scripts are:
"scripts": {
...
"migrate": "tsx ./src/server/db/migrator.ts"
},
"scripts": {
...
"migrate": "tsx ./src/server/db/migrator.ts"
},
When running pnpm run migrate I'm getting:
❌ Invalid environment variables: {
DATABASE_URL: [ 'Required' ],
DISCORD_CLIENT_ID: [ 'Required' ],
DISCORD_CLIENT_SECRET: [ 'Required' ]
}
/Users/erikashepa/Projects/budget/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/core/index.ts:217
throw new Error("Invalid environment variables");
^


Error: Invalid environment variables
at error (/Users/erikashepa/Projects/budget/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/core/index.ts:217:13)
at b (/Users/erikashepa/Projects/budget/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/core/index.ts:229:12)
at P (/Users/erikashepa/Projects/budget/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-nextjs/index.ts:73:10)
at file:///Users/erikashepa/Projects/budget/src/env.js:4:20
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

Node.js v18.17.1
❌ Invalid environment variables: {
DATABASE_URL: [ 'Required' ],
DISCORD_CLIENT_ID: [ 'Required' ],
DISCORD_CLIENT_SECRET: [ 'Required' ]
}
/Users/erikashepa/Projects/budget/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/core/index.ts:217
throw new Error("Invalid environment variables");
^


Error: Invalid environment variables
at error (/Users/erikashepa/Projects/budget/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/core/index.ts:217:13)
at b (/Users/erikashepa/Projects/budget/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/core/index.ts:229:12)
at P (/Users/erikashepa/Projects/budget/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@t3-oss/env-nextjs/index.ts:73:10)
at file:///Users/erikashepa/Projects/budget/src/env.js:4:20
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

Node.js v18.17.1
Erik.
Create T3 App
Prisma 🚀 Create T3 App
The best way to start a full-stack, typesafe Next.js app.
1 Reply
erikash
erikashOP12mo ago
Anyone successfuly ran a command using tsx?
Want results from more Discord servers?
Add your server