Importing mjs files in "non project/standalone" TS files
I've setup my project using create t3 app & am therefore exporting all my env variables from typesafe
env.mjs
file.
Importing and using them works flawlessly for all "project files" but it doesn't for some standalone ts files.
E.g. I have a file db/seed/seed.ts
which I manually run using npm run db:seed
when needed.
In this file I'd like to use the DB path from my env.mjs
but it gives me the following errors when using import { env } from '@/env.mjs';
:
Error [ERR_REQUIRE_ESM]: require() of ES Module /.../src/env.mjs not supported. Instead change the require of /.../src/env.mjs to a dynamic import() which is available in all CommonJS modules. ...My
db:run
script: npx ts-node -r tsconfig-paths/register -O {\\\"module\\\":\\\"CommonJS\\\"} db/seed/seed.ts
And here's my tsconfig.json
:
I know that this is not an issue with "create t3" itself, but I thought that you guys probably know best how to solve this since you know the structure of those env.mjs
files & why they are built that way etc.
I'd really appreciate any input on how to solve this & make env.mjs
available in my standalone TS files as well!0 Replies