dotenv
library is a popular choice for loading environment variables from a .env
file into process.env
. However, many developers encounter issues where dotenv
seems not to work as expected in TypeScript projects. This blog post aims to explore the root causes of these problems, provide usage methods, common practices, and best practices to help you resolve these issues and effectively use dotenv
in your TypeScript applications.dotenv
is a zero-dependency module that loads environment variables from a .env
file into process.env
. This allows you to keep your application’s configuration separate from your codebase, which is especially useful for sensitive information like API keys, database passwords, etc.
dotenv
incorrectly, it won’t work as expected.dotenv
needs to be loaded before any code that depends on the environment variables. If it’s loaded too late, the variables won’t be available when needed.process.env
object correctly, leading to type errors.First, install dotenv
in your TypeScript project using npm or yarn:
npm install dotenv
# or
yarn add dotenv
Create a .env
file in the root of your project:
# .env
API_KEY=your_api_key
DB_PASSWORD=your_db_password
In your TypeScript file, import and configure dotenv
at the top:
import dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();
// Access environment variables
const apiKey = process.env.API_KEY;
const dbPassword = process.env.DB_PASSWORD;
console.log(apiKey);
console.log(dbPassword);
dotenv
as early as possible in your application, preferably at the entry point.process.env
: To avoid type errors, you can create a custom type definition for process.env
.declare global {
namespace NodeJS {
interface ProcessEnv {
API_KEY: string;
DB_PASSWORD: string;
}
}
}
import dotenv from 'dotenv';
dotenv.config();
const apiKey: string = process.env.API_KEY;
const dbPassword: string = process.env.DB_PASSWORD;
.env
file is not in the root directory, you need to specify the path when calling dotenv.config()
.import dotenv from 'dotenv';
dotenv.config({ path: './config/.env' });
undefined
before using them.import dotenv from 'dotenv';
dotenv.config();
const apiKey = process.env.API_KEY;
if (apiKey) {
// Use the API key
} else {
console.error('API key is not set');
}
Instead of accessing process.env
directly throughout your codebase, create a configuration file to manage your environment variables.
// config.ts
import dotenv from 'dotenv';
dotenv.config();
const config = {
apiKey: process.env.API_KEY || '',
dbPassword: process.env.DB_PASSWORD || ''
};
export default config;
// main.ts
import config from './config';
console.log(config.apiKey);
console.log(config.dbPassword);
.env
Out of Version ControlAdd .env
to your .gitignore
file to prevent sensitive information from being committed to your repository.
In conclusion, dotenv
is a powerful tool for managing environment variables in TypeScript projects. However, issues can arise due to incorrect import, timing of loading, or TypeScript configuration. By following the usage methods, common practices, and best practices outlined in this blog post, you can effectively use dotenv
in your TypeScript applications and avoid the common pitfalls.