Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using import.meta.env fails inside of a trigger.dev build #1333

Open
ericallam opened this issue Sep 20, 2024 · 3 comments
Open

Using import.meta.env fails inside of a trigger.dev build #1333

ericallam opened this issue Sep 20, 2024 · 3 comments

Comments

@ericallam
Copy link
Member

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.

@ericallam
Copy link
Member Author

Hacky solution:

  • Add @import-meta-env/unplugin as a devDependency
  • If you don't already have it, add @trigger.dev/build as 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;

@the-dream-machine
Copy link

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",

@JohnCido
Copy link

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/unplugin as a devDependency
  • If you don't already have it, add @trigger.dev/build as 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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants