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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *