nodejs-env-loader v1.0.4
nodejs-env-loader
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.
Features
- 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.
Installation
From npm
To install the package from npm, run:
npm install nodejs-env-loaderUsing Different Package Managers
yarn (Fast, reliable, and secure dependency management)
yarn add nodejs-env-loaderpnpm (Efficient package manager with disk space optimization)
pnpm add nodejs-env-loaderbun (Modern JavaScript runtime with built-in package manager)
bun add nodejs-env-loaderLocal Installation
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
Usage
Create a
.envFile: In the root of your project, create a.envfile containing your environment variables:# .env DATABASE_URL=your-database-url API_KEY=your-api-key PORT=3000 DEBUG_MODE=trueLoad 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);
API
load(options)
Loads environment variables from a specified .env file into process.env.
Parameters
| 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. |
Returns
| Property | Type | Description |
|---|---|---|
parsed | object | Key-value pairs from the .env file. |
error | Error | Error object if the .env file could not be loaded. |
Example
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);
}Important Notes
- Protected Environment Variables: The loader prevents overwriting critical system environment variables such as
PATH,HOME,USER, etc., unless explicitly allowed via theoverrideoption. - File Security: To maintain security, the loader restricts loading
.envfiles outside the current working directory. - Cross-Platform Compatibility: Automatically normalizes line endings for different operating systems.
Contributing
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.
License
This project is licensed under the MIT License. See the LICENSE file for details.
For more information, visit the GitHub repository.