skaface
skaface
TTCTheo's Typesafe Cult
Created by skaface on 9/21/2023 in #questions
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:
{
"compilerOptions": {
"target": "es2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"checkJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"noUncheckedIndexedAccess": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": [".eslintrc.cjs", "next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.cjs", "**/*.mjs"],
"exclude": ["node_modules"]
}
{
"compilerOptions": {
"target": "es2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"checkJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"noUncheckedIndexedAccess": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": [".eslintrc.cjs", "next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.cjs", "**/*.mjs"],
"exclude": ["node_modules"]
}
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!
1 replies