nodejs-env-loader is a Node.js utility that simplifies the loading of environment variables from a .env
file into your application's process.env
. This approach enhances the management of configuration settings, especially secrets, without hardcoding them into your source code.
- Secure Loading: Ensures that environment variables are loaded without overwriting critical system variables.
- Customizable: Allows specifying custom paths, encoding, and override options.
- Safe File Access: Prevents loading files outside the current working directory to maintain security.
- Cross-Platform Support: Handles different newline formats (Windows, Unix) seamlessly.
To install the package from npm, run:
npm install nodejs-env-loader
yarn add nodejs-env-loader
pnpm add nodejs-env-loader
bun add nodejs-env-loader
If you prefer to install it locally:
- Clone the repository:
git clone https://github.com/CodeItAftab/nodejs-env-loader.git
- Navigate to the project directory:
cd nodejs-env-loader
- Install the dependencies:
npm install
- Link the package globally for local testing:
npm link
- Use the package in another local project:
cd ../your-project npm link nodejs-env-loader
-
Create a
.env
File: In the root of your project, create a.env
file containing your environment variables:# .env DATABASE_URL=your-database-url API_KEY=your-api-key PORT=3000 DEBUG_MODE=true
-
Load Environment Variables: In your application entry point (e.g.,
index.js
), load the environment variables usingnodejs-env-loader
:const { load } = require('nodejs-env-loader'); const result = load(); if (result.error) { console.error('Error loading .env file:', result.error); } else { console.log('Environment variables loaded:', result.parsed); } // Now you can access your environment variables via process.env console.log('Database URL:', process.env.DATABASE_URL);
Loads environment variables from a specified .env
file into process.env
.
Parameter | Type | Default | Description |
---|---|---|---|
path |
string | .env |
Path to the .env file. |
override |
boolean | false |
If true , existing environment variables will be overwritten. |
encoding |
string | utf-8 |
File encoding. |
Property | Type | Description |
---|---|---|
parsed |
object | Key-value pairs from the .env file. |
error |
Error | Error object if the .env file could not be loaded. |
const { load } = require('nodejs-env-loader');
const result = load({ path: './config/.env', override: true });
if (result.error) {
console.error('Error loading .env file:', result.error);
} else {
console.log('Environment variables loaded:', result.parsed);
}
-
Protected Environment Variables: The loader prevents overwriting critical system environment variables such as
PATH
,HOME
,USER
, etc., unless explicitly allowed via theoverride
option. -
File Security: To maintain security, the loader restricts loading
.env
files outside the current working directory. - Cross-Platform Compatibility: Automatically normalizes line endings for different operating systems.
We welcome contributions! Follow these steps to contribute:
- Fork the Repository: Click the "Fork" button on the repository page.
-
Clone Your Fork:
git clone https://github.com/your-username/nodejs-env-loader.git
-
Create a Branch:
git checkout -b my-feature-branch
-
Make Changes & Commit:
git add . git commit -m "Add new feature"
-
Push to Your Fork:
git push origin my-feature-branch
- Create a Pull Request: Go to the original repository on GitHub and open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
For more information, visit the GitHub repository.