Environment Variables TypeScript

No, this is not possible.

If your TypeScript file runs in Node, you can use process.env since it’s a regular Node API – but in that case, the environment variable is accessed at runtime, by your compiled file, not at compile time by TypeScript.

If your TypeScript file runs in the browser, then there is no process.env, so you cannot even access it at runtime. You could use a tool like Webpack to replace references to process.env variables in the compiled file with their respective values at build time – but that still is not TypeScript’s doing.

So unfortunately the answer is no: TypeScript cannot do this, and you’ll have to use something else (like Webpack, or even just a search and replace) to inject environment variables into your script.

Leave a Comment