1.0.4 • Published 5 months ago

nodejs-env-loader v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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-loader

Using Different Package Managers

yarn (Fast, reliable, and secure dependency management)

yarn add nodejs-env-loader

pnpm (Efficient package manager with disk space optimization)

pnpm add nodejs-env-loader

bun (Modern JavaScript runtime with built-in package manager)

bun add nodejs-env-loader

Local Installation

If you prefer to install it locally:

  1. Clone the repository:
    git clone https://github.com/CodeItAftab/nodejs-env-loader.git
  2. Navigate to the project directory:
    cd nodejs-env-loader
  3. Install the dependencies:
    npm install
  4. Link the package globally for local testing:
    npm link
  5. Use the package in another local project:
    cd ../your-project
    npm link nodejs-env-loader

Usage

  1. 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
  2. Load Environment Variables: In your application entry point (e.g., index.js), load the environment variables using nodejs-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

ParameterTypeDefaultDescription
pathstring.envPath to the .env file.
overridebooleanfalseIf true, existing environment variables will be overwritten.
encodingstringutf-8File encoding.

Returns

PropertyTypeDescription
parsedobjectKey-value pairs from the .env file.
errorErrorError 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 the override 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.

Contributing

We welcome contributions! Follow these steps to contribute:

  1. Fork the Repository: Click the "Fork" button on the repository page.
  2. Clone Your Fork:
    git clone https://github.com/your-username/nodejs-env-loader.git
  3. Create a Branch:
    git checkout -b my-feature-branch
  4. Make Changes & Commit:
    git add .
    git commit -m "Add new feature"
  5. Push to Your Fork:
    git push origin my-feature-branch
  6. 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.

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago