npm stats
  • Search
  • About
  • Repo
  • Sponsor
  • more
    • Search
    • About
    • Repo
    • Sponsor

Made by Antonio Ramirez

bare-dotenv

1.0.1

@mafintosh

npmHomeRepoSnykSocket
Downloads:6
$ npm install bare-dotenv
DailyWeeklyMonthlyYearly

bare-dotenv NPM version

bare-dotenv

bare-dotenv is a lightweight adaptation of dotenv specifically designed for the Bare runtime. It loads environment variables from a .env file into the Bare environment using bare-env.

🎯 Key Differences from Original dotenv

  • Bare Runtime Support: Designed specifically for Bare, a lightweight JavaScript runtime
  • No process.env: Uses bare-env instead of Node.js's process.env (which doesn't exist in Bare)
  • Simplified API: Removed encryption/decryption features to keep it lightweight
  • No Runtime Preloading: Bare doesn't support the -r preload flag like Node.js
  • Lightweight: Focused on core functionality without heavy dependencies

🌱 Install

npm install bare-dotenv --save

🏗️ Usage

Basic Usage

Create a .env file in the root of your project:

S3_BUCKET="YOURS3BUCKET"
SECRET_KEY="YOURSECRETKEYGOESHERE"

As early as possible in your application, import and configure bare-dotenv:

require("bare-dotenv").config();

Or using ES6:

import "bare-dotenv/config";

That's it! Your environment variables are now available through bare-env:

const env = require("bare-env");

console.log(env.S3_BUCKET); // "YOURS3BUCKET"
console.log(env.SECRET_KEY); // "YOURSECRETKEYGOESHERE"

Advanced Usage

const dotenv = require("bare-dotenv");

// Load with custom options
const result = dotenv.config({
  path: "/custom/path/to/.env",
  debug: true,
  override: true,
});

if (result.error) {
  throw result.error;
}

console.log(result.parsed);

Multiple .env Files

const dotenv = require("bare-dotenv");

// Load multiple files in order
dotenv.config({
  path: [".env.local", ".env"],
});

📖 API Reference

config(options)

Loads environment variables from .env file(s) into the Bare environment.

Options

  • path (string|array): Path to .env file(s). Default: '.env'
  • encoding (string): File encoding. Default: 'utf8'
  • debug (boolean): Enable debug logging. Default: false
  • override (boolean): Override existing environment variables. Default: false
  • quiet (boolean): Suppress logging. Default: true

Example

const dotenv = require("bare-dotenv");

const result = dotenv.config({
  path: ".env.production",
  debug: true,
  override: true,
});

if (result.error) {
  console.error("Error loading .env file:", result.error);
}

parse(src)

Parse environment variables from a string or buffer.

const dotenv = require("bare-dotenv");

const parsed = dotenv.parse("HELLO=world\nFOO=bar");
console.log(parsed); // { HELLO: 'world', FOO: 'bar' }

populate(target, parsed, options)

Populate a target object with parsed environment variables.

const dotenv = require("bare-dotenv");
const env = require("bare-env");

const parsed = { HELLO: "world" };
dotenv.populate(env, parsed, { override: true });

🔧 Configuration via Environment Variables

You can configure bare-dotenv using environment variables:

# Set custom path
DOTENV_CONFIG_PATH=/custom/path/.env

# Enable debug mode
DOTENV_CONFIG_DEBUG=true

# Override existing variables
DOTENV_CONFIG_OVERRIDE=true

# Set encoding
DOTENV_CONFIG_ENCODING=latin1

# Suppress logging
DOTENV_CONFIG_QUIET=true

📝 .env File Format

Basic Variables

# Basic key-value pairs
S3_BUCKET="YOURS3BUCKET"
SECRET_KEY="YOURSECRETKEYGOESHERE"
DATABASE_URL="postgres://user:pass@localhost/db"

Multiline Values

# Multiline values (>= v15.0.0)
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA...
-----END RSA PRIVATE KEY-----"

Comments

# This is a comment
SECRET_KEY=YOURSECRETKEYGOESHERE # inline comment
SECRET_HASH="something-with-a-#-hash"

Quoted Values

# Single quotes
SINGLE_QUOTE='quoted value'

# Double quotes (supports \n for newlines)
MULTILINE="new\nline"

# Backticks
BACKTICK_KEY=`This has 'single' and "double" quotes inside`

🚀 Examples

Basic Application

// index.js
require("bare-dotenv").config();

const env = require("bare-env");

console.log(`Hello ${env.USER_NAME}!`);
console.log(`Database: ${env.DATABASE_URL}`);

With Custom Options

// config.js
const dotenv = require("bare-dotenv");
const env = require("bare-env");

// Load environment-specific config
const envType = env.BARE_ENV || "development";
dotenv.config({
  path: `.env.${envType}`,
  debug: true,
  override: true,
});

Testing

// test.js
const test = require("brittle");
const dotenv = require("bare-dotenv");

test("loads environment variables", function (t) {
  dotenv.config({ path: "tests/.env.test" });

  const env = require("bare-env");
  t.is(env.TEST_VAR, "test_value");
});

❓ FAQ

Why doesn't bare-dotenv support encryption?

Bare-dotenv is designed to be lightweight and focused on core functionality. Encryption features were removed to keep the module simple and avoid dependencies on bare-crypto which may not support all required crypto methods.

Why no runtime preloading?

Bare runtime doesn't support the -r preload flag like Node.js. You need to explicitly require and configure bare-dotenv in your application code.

How is this different from the original dotenv?

  • Runtime: Designed for Bare instead of Node.js
  • Environment: Uses bare-env instead of process.env
  • Features: Removed encryption, preloading, and other Node.js-specific features
  • Size: Lighter weight with fewer dependencies

Can I use this with Node.js?

No, this module is specifically designed for the Bare runtime. For Node.js, use the original dotenv package.

What about variable expansion?

Variable expansion is not supported in bare-dotenv. Consider using dotenvx for advanced features like variable expansion and encryption.

🤝 Contributing

See CONTRIBUTING.md

📄 License

See LICENSE

🙏 Acknowledgments

This project is based on the original dotenv by motdotla, adapted for the Bare runtime.

📊 Who's using bare-dotenv?

Projects that use bare-dotenv often use the keyword "bare-dotenv" on npm.