Introduction to dotenv-run
dotenv-run
is a versatile package that simplifies the process of loading environment variables into applications by leveraging the popular dotenv
library. This tool is essentially a collection of packages that extend the capabilities of dotenv
, allowing seamless integration with various environments and tools. It enhances the management of environment variables, providing a secure and efficient operational framework for developers working on both simple and complex projects, including those structured as monorepos.
Key Features
-
Monorepo Support:
dotenv-run
is tailored for monorepo projects, where multiple applications reside within a single repository. This feature helps in managing shared configurations across different applications efficiently. -
Universal Compatibility: The tool supports a broad range of integrations including CLI, Webpack, Rollup, Vite, ESbuild, and Angular, making it highly adaptable and versatile for different project needs.
-
TypeScript Support: For TypeScript projects,
dotenv-run
offers type definitions that enhance type safety and code clarity when accessing environment variables viaprocess.env
orimport.meta.env
. -
ESM Modules Support: It fully supports the use of environment variables within ECMAScript Modules (ESM), which is increasingly important as many projects shift towards modern JavaScript standards.
-
Security: The package allows filtering of environment variables by using prefixes, ensuring that only necessary data is exposed or utilized, which adds a layer of security to your applications.
Integrations
The following are key integrations supported by dotenv-run
, each designed to cater to specific environments and tools:
- CLI: A command-line interface package that allows scripts to be run with the loaded environment variables.
- Core: The foundational package used for loading variables from
.env
files, suitable for various projects. - ESBuild, Rollup, Webpack: Plugins designed to inject environment variables into applications built using these popular bundling tools.
- Angular: The @ngx-env/builder package provides an easy way to manage environment variables for Angular applications.
Quick Start
Setting up dotenv-run
typically involves configuring environment variables within a project directory, often in a structured monorepo setup. This organization allows for centralized and consistent configuration management across multiple microservices or applications within a single workspace.
CLI Example
The @dotenv-run/cli
package allows developers to execute commands with the environmental context set by .env
files:
npx dotenv-run -- npm start
This command initializes npm scripts with the appropriate environment variables without requiring manual configuration.
Core Example
Using the core package directly involves JavaScript or TypeScript code where the environment is configured programmatically:
env({
root: "../..",
verbose: true,
prefix: "^API_",
files: [".env"],
});
This piece of code configures the environment by setting root directories and specifying the files to load.
Angular Integration
The Angular integration with @ngx-env/builder
is particularly noteworthy as it wraps the functionality of both Webpack and ESBuild plugins. It automatically injects environment variables into Angular apps, making it official in various reputable projects.
ng add @ngx-env/builder
This command integrates the builder into Angular projects, requiring minimal setup to start leveraging the environment variables.
Further Information
For developers looking to delve deeper into dotenv-run
, the documentation offers comprehensive guides and specific use-cases that showcase its ability to streamline workflows and enhance the configuration management of diverse environments.
Acknowledgements
The core idea behind dotenv-run
builds on the success of the original dotenv
project by Motdotla, enhancing its capabilities for modern development needs.
License
dotenv-run
is open-sourced under the MIT License, ensuring that it remains free and accessible for a wide range of development projects, encouraging collaboration and enhancement by the global developer community.