Categories
TypeScript Answers

How to add environment variable with dotenv and TypeScript?

Spread the love

Sometimes, we want to add environment variable with dotenv and TypeScript.

In this article, we’ll look at how to add environment variable with dotenv and TypeScript.

How to add environment variable with dotenv and TypeScript?

To add environment variable with dotenv and TypeScript, we import the dotenv module.

And then we call dotenv.config to load the enviroment variables values from the path specified to load them into process.env.

For instance, we write

import * as dotenv from "dotenv";
dotenv.config({ path: __dirname + "/.env" });

to load the environment variables from __dirname + "/.env" with dotenv.config.

And then the environment variables should be available as properties in process.env.

Conclusion

To add environment variable with dotenv and TypeScript, we import the dotenv module.

And then we call dotenv.config to load the enviroment variables values from the path specified to load them into process.env.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.