How to make scripts in a t3-stack app (with tsx and t3-env)?

I am trying to create a scripts folder to run things along my website and create tools, test things and all. I tried tsx with success, until I needed the env to be available (thus involving t3-env). I am currently trying to do a small script to test inserting things in my database:
import { db } from "~/server/db";
import { food } from "~/server/db/schema";

await db.insert(food).values({
name: "Banana",
});
import { db } from "~/server/db";
import { food } from "~/server/db/schema";

await db.insert(food).values({
name: "Banana",
});
Until now, I did scripts that did not involve resources from the app but right now I would like to connect to my database but I get this error:
:x: Invalid environment variables: {
DATABASE_URL: [ 'Required' ],
DATABASE_HOST: [ 'Required' ],
DATABASE_USERNAME: [ 'Required' ],
DATABASE_PASSWORD: [ 'Required' ],
}
.../node_modules/.pnpm/@[email protected]_ophst5sms6obxk2veobtcst52a/node_modules/@t3-oss/core/index.ts:217
throw new Error("Invalid environment variables");
:x: Invalid environment variables: {
DATABASE_URL: [ 'Required' ],
DATABASE_HOST: [ 'Required' ],
DATABASE_USERNAME: [ 'Required' ],
DATABASE_PASSWORD: [ 'Required' ],
}
.../node_modules/.pnpm/@[email protected]_ophst5sms6obxk2veobtcst52a/node_modules/@t3-oss/core/index.ts:217
throw new Error("Invalid environment variables");
Solution:
For anyone looking for a solution... I managed to get it working after multiple try with the following setup: 1 - you will need the dotenv package 2 - my package.json look like this: ```json...
Jump to solution
1 Reply
Solution
lelabo
lelabo11mo ago
For anyone looking for a solution... I managed to get it working after multiple try with the following setup: 1 - you will need the dotenv package 2 - my package.json look like this:
{
"name": "project",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {
"build": "next build",
[...]
"tsx": "dotenv tsx",
"dev": "next dev --turbo",
"lint": "next lint",
"start": "next start"
},
[...]
}
{
"name": "project",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {
"build": "next build",
[...]
"tsx": "dotenv tsx",
"dev": "next dev --turbo",
"lint": "next lint",
"start": "next start"
},
[...]
}
WIth this, I am able to run scripts with the following command: pnpm run tsx scripts/any-script.ts
Want results from more Discord servers?
Add your server