Effortlessly Load Environment Variables in package.json Scripts with dotenv

Why Load Environment Variables in Your package.json Scripts?

If you’ve ever developed a Node.js or TypeScript application, you’re probably familiar with environment variables. These are essential for managing sensitive information, such as API keys and database credentials, without hardcoding them into your code. While using .env files to store these variables is common practice, ensuring they load correctly when running scripts from package.json can be a bit tricky.

Thank me by sharing on Twitter 🙏

For example, if you’re running a TypeScript project, you might have a script like "dev": "tsx watch src/index.ts", which starts the TypeScript watcher. But without loading environment variables, your app might fail to run or operate without access to key information. By integrating dotenv directly into these scripts, we can make sure all required variables are loaded smoothly, right before each script runs. Here’s how I set it up.

How to Set Up dotenv in Your package.json Scripts

Let’s dive in and set this up step-by-step.

Step 1: Install dotenv-cli

To load environment variables easily, I’m using a package called dotenv-cli, which extends the power of dotenv to our command-line scripts. To install it, run:

ShellScript
npm install dotenv-cli --save-dev

This package lets us specify .env files directly in our scripts, ensuring the variables load before the main command starts.

Step 2: Add dotenv to Your package.json Script

In package.json, locate the scripts section. I wanted to load my environment variables before starting the TypeScript watcher, so I edited the script as follows:

{
  "scripts": {
    "dev": "dotenv -e .env -- tsx watch src/index.ts"
  }
}

This command does two things:

  1. dotenv -e .env -- loads the variables from .env.
  2. tsx watch src/index.ts starts the TypeScript watcher, with access to any environment variables loaded by dotenv.

Step 3 (Optional): For Cross-Platform Compatibility, Use cross-env

Some setups require additional compatibility, especially on Windows. By adding cross-env, I can ensure compatibility across different environments.

To install cross-env, run:

ShellScript
npm install cross-env --save-dev

Then, update the script as follows:

{
  "scripts": {
    "dev": "cross-env $(dotenv -e .env) tsx watch src/index.ts"
  }
}

This allows the script to load environment variables in a way that’s compatible with both Windows and Unix-based systems.

Conclusion

Integrating dotenv-cli into your package.json scripts makes loading environment variables quick and hassle-free. Now, whenever I run npm run dev, my .env variables are automatically loaded, keeping my sensitive data secure and my code clean. This setup saves time and avoids issues with missing environment variables, so I can focus on building features instead of troubleshooting configuration.

Give this approach a try and see how it simplifies your workflow!

Share this:

Leave a Reply