Environment variables in command line script outside

I'm trying to create a simple command line script (ultimately to load my database, but I've not got that far yet). I'm hitting a 'Invalid environment variables' problem.
env-test.ts: import nextEnv from "@next/env"; nextEnv.loadEnvConfig(process.cwd()); console.log("DATABASE_URL:", process.env.DATABASE_URL); import { env } from "~/env"; Output: $ npx tsx src/scripts/osmimport/env-test-ts.ts ❌ Invalid environment variables: [ { code: 'invalid_type', expected: 'string', received: 'undefined', path: [ 'AUTH_DISCORD_ID' ], message: 'Required' }, { code: 'invalid_type', expected: 'string', received: 'undefined', path: [ 'AUTH_DISCORD_SECRET' ], message: 'Required' }, { code: 'invalid_type', expected: 'string', received: 'undefined', path: [ 'DATABASE_URL' ], message: 'Required' } ] file:///.../node_modules/.pnpm/@t3-oss+env-core@0.13.6_arktype@2.1.20_typescript@5.8.3_zod@3.25.30/node_modules/@t3-oss/env-core/dist/src-Bb3GbGAa.js:54 throw new Error("Invalid environment variables"); If I comment out thelast line so it doesn't load the standard t3-create-app generated env.js file, the console.log runs and shows loadEnvConfig is actually loading my .env. It's as-if env.js is imported before the environment is loaded. Is there a known-good example of a command line script that can be run like this, outside of next but using .env? I'm missing something likely obvious.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?