DEV Community

nuzulfikrie
nuzulfikrie

Posted on

1

How to add .env in your typescript projects.

This is a simple guide on how to use .env in a typescript project.

I am starting to learn typescript, so hopefully this will be useful to all newbie programmers using typescript out there

  1. Install dotenv library.

npm install dotenv
Enter fullscreen mode Exit fullscreen mode
  1. Include inside the file that will make use of the environment variables.
import * as dotenv from "dotenv";

dotenv.config();
Enter fullscreen mode Exit fullscreen mode
  1. Call the variables in files, like so.


export const configDev = {
  developerName: process.env.developerName,
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay