dotenv
is a popular npm package that simplifies the process of loading environment variables from a .env
file into your Node.js application. When combined with TypeScript, it provides a type - safe way to access these variables. In this blog post, we will explore the fundamental concepts, usage methods, common practices, and best practices of using dotenv
with TypeScript..env
FileA .env
file is a simple text file where you can define your environment variables in the KEY=VALUE
format. For example:
DB_USER=myuser
DB_PASSWORD=mypassword
API_KEY=1234567890abcdef
This file is typically placed in the root directory of your project.
dotenv
PackageThe dotenv
package reads the .env
file and loads its contents into the process.env
object in Node.js. This way, you can access the environment variables throughout your application.
TypeScript is a superset of JavaScript that adds static typing to the language. When using dotenv
with TypeScript, we can create type definitions for our environment variables to ensure type safety.
First, make sure you have Node.js and npm (or yarn) installed on your machine. Then, create a new TypeScript project and install the necessary dependencies:
mkdir dotenv-typescript-example
cd dotenv-typescript-example
npm init -y
npm install dotenv
npm install --save-dev typescript @types/node
npx tsc --init
.env
FileCreate a .env
file in the root directory of your project with the following content:
PORT=3000
API_URL=https://api.example.com
dotenv
In your main TypeScript file (e.g., index.ts
), import and configure dotenv
:
import dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();
// Access environment variables
const port = process.env.PORT;
const apiUrl = process.env.API_URL;
console.log(`Port: ${port}`);
console.log(`API URL: ${apiUrl}`);
Compile your TypeScript code using the TypeScript compiler:
npx tsc
Then, run the generated JavaScript file:
node dist/index.js
To make our environment variables type - safe, we can create a custom type definition for them.
Create a new file named env.d.ts
in your project:
declare global {
namespace NodeJS {
interface ProcessEnv {
PORT: string;
API_URL: string;
}
}
}
export {};
Now, when you access the environment variables, TypeScript will enforce the types:
import dotenv from 'dotenv';
dotenv.config();
// TypeScript will ensure that PORT and API_URL are strings
const port: string = process.env.PORT;
const apiUrl: string = process.env.API_URL;
console.log(`Port: ${port}`);
console.log(`API URL: ${apiUrl}`);
.env
FilesFor different environments (e.g., development, testing, production), you can use multiple .env
files. For example, create .env.development
, .env.test
, and .env.production
files. Then, in your script, you can specify which file to load based on the environment:
import dotenv from 'dotenv';
const envFile = `.env.${process.env.NODE_ENV || 'development'}`;
dotenv.config({ path: envFile });
When accessing environment variables, it’s a good practice to check if they are defined. For example:
import dotenv from 'dotenv';
dotenv.config();
const apiKey = process.env.API_KEY;
if (!apiKey) {
throw new Error('API_KEY environment variable is not defined');
}
console.log(`API Key: ${apiKey}`);
.env
file to version control: The .env
file may contain sensitive information. Add it to your .gitignore
file to prevent it from being pushed to the repository.// env.ts
import dotenv from 'dotenv';
dotenv.config();
export const PORT = process.env.PORT || '3000';
export const API_URL = process.env.API_URL;
// index.ts
import { PORT, API_URL } from './env';
console.log(`Port: ${PORT}`);
console.log(`API URL: ${API_URL}`);
Using dotenv
with TypeScript is a powerful way to manage environment variables in a type - safe and secure manner. By following the concepts, usage methods, common practices, and best practices outlined in this blog post, you can build more robust and flexible applications. Remember to keep your environment variables secure and well - organized to ensure the smooth development and deployment of your projects.