Using import.meta.env fails inside of a trigger.dev build
From what I can tell, import.meta.env is a vite thing that populates import.meta.env with environment variables.
https://import-meta-env.org/guide/getting-started/introduction.html
An example usage is here:
https://github.com/usetonearm/core-web/blob/d178a6277306e9bfe0cde49c6550ceae33475292/packages/supabase/src/get-supabase-client-keys.ts#L6
They do have an esbuild plugin that we should look into seeing if it solves the issue.
Hacky solution:
- Add
@import-meta-env/unpluginas a devDependency - If you don't already have it, add
@trigger.dev/buildas a devDependency
Then update your trigger.config.ts file like so:
import { defineConfig } from "@trigger.dev/sdk/v3";
import { esbuildPlugin } from "@trigger.dev/build";
import { esbuild as importMetaEnvPlugin } from "@import-meta-env/unplugin";
export default defineConfig({
project: "<your project ref>",
build: {
extensions: [
esbuildPlugin(
importMetaEnvPlugin({
example: ".env.example",
env: ".env",
}) as any
),
],
},
});
(globalThis as any).import_meta_env = process.env;
Did you test this solution? I'm not able to get it to recognize the import.meta.env variables. It throws the error:
Cannot read properties of undefined (reading 'VITE_CLOUDFLARE_R2_SECRET_ACCESS_KEY')
I tried messing around with the plugin's transformMode and a few other options, but I still couldn't get it to work.
Library versions:
vite="5.1.0",
@trigger.dev/sdk="3.0.12",
You can actually transpile all the envs at the build time using this option:
importMetaEnvPlugin({
env: './.env',
example: './.env.example',
transformMode: 'compile-time',
})
So you don't have to add the last line to expose it again.
Hacky solution:
- Add
@import-meta-env/unpluginas a devDependency- If you don't already have it, add
@trigger.dev/buildas a devDependencyThen update your trigger.config.ts file like so:
import { defineConfig } from "@trigger.dev/sdk/v3"; import { esbuildPlugin } from "@trigger.dev/build"; import { esbuild as importMetaEnvPlugin } from "@import-meta-env/unplugin"; export default defineConfig({ project: "<your project ref>", build: { extensions: [ esbuildPlugin( importMetaEnvPlugin({ example: ".env.example", env: ".env", }) as any ), ], }, }); (globalThis as any).import_meta_env = process.env;