Rollup replace plugin with typescript can't find variable name

Viewed 359

I am using @rollup/plugin-replace to use dotenv in Svelte.

When I replace the variables like below,

replace({
  preventAssignment: true,
  DATABASE_URL: JSON.stringify(process.env.SUPABASE_URL),
})

and use above in main.ts, it can't find the variable but prints well in the console.

enter image description here

The DATABASE_URL actaully exists. It is just a typescript error. How can I resolve this?

2 Answers

replace is going to do a literal string find-and-replace, so if you define your configuration like:

replace({
  preventAssignment: true,
  DATABASE_URL: process.env.SUPABASE_URL,
})

you should then treat DATABASE_URL in your code as a value (i.e., not like a variable). So:

const url = "DATABASE_URL";
connect(url, ...);
Related