@dotenvage/node

中文文档
0.8.25发布于 2026/11/13MIT OR Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 4462

Node.js bindings for dotenvage - Dotenv with age encryption

原始使用文档
文档目录22 节
  1. Features
  2. Installation
  3. Quick Start
  4. Basic Usage (Like dotenv)
  5. Get Variables as Object (Non-mutating)
  6. Encrypt and Decrypt Values
  7. API Reference
  8. JsEnvLoader
  9. JsSecretManager
  10. Utility Functions
  11. CLI Usage
  12. Next.js Integration
  13. TypeScript Examples
  14. File Layering
  15. Key Management
  16. Setting the Encryption Key
  17. Generating a Key
  18. Comparison with dotenv
  19. Requirements
  20. Building from Source
  21. License
  22. Links

@dotenvage/node

npm version npm downloads License: MIT OR Apache-2.0

Node.js bindings for dotenvage - Dotenv with age encryption.

Features

  • 🔒 Encrypt secrets in .env files - Safely commit encrypted secrets to version control
  • 📦 Native performance - Built with NAPI-RS for fast Rust performance
  • 🔄 Auto-detection - Automatically identifies which keys should be encrypted
  • 🌳 File layering - Multiple .env* files with automatic precedence
  • 💻 CLI support - Includes dotenvage command-line tool
  • 📝 TypeScript support - Full TypeScript definitions included

Installation

npm install @dotenvage/node

Quick Start

Basic Usage (Like dotenv)

import * as dotenvage from "@dotenvage/node";

// Load encrypted .env files into process.env
const loader = dotenvage.JsEnvLoaderNew();
loader.load(); // Mutates process.env (like dotenv.config())

// Now access variables
const apiKey = process.env.API_KEY;
const dbUrl = process.env.DATABASE_URL;

Get Variables as Object (Non-mutating)

import * as dotenvage from "@dotenvage/node";

const loader = dotenvage.JsEnvLoaderNew();
const env = loader.getAllVariables(); // Returns Record<string, string>

// Use without modifying process.env
console.log(env.API_KEY);
console.log(env.DATABASE_URL);

Encrypt and Decrypt Values

import * as dotenvage from "@dotenvage/node";

// Generate a new key pair
const manager = dotenvage.JsSecretManagerGenerate();
const publicKey = manager.publicKeyString(); // Share this public key

// Encrypt a secret
const secret = "sk_live_abc123";
const encrypted = manager.encryptValue(secret);
// Returns: "ENC[AGE:b64:...]"

// Decrypt it back
const decrypted = manager.decryptValue(encrypted);
console.log(decrypted === secret); // true

API Reference

JsEnvLoader

Loads and decrypts .env files.

JsEnvLoaderNew(): JsEnvLoader

Creates a new loader with a default SecretManager.

const loader = dotenvage.JsEnvLoaderNew();

load(): void

Loads .env files from the current directory into process.env.

loader.load(); // Sets variables in process.env

loadFromDir(dir: string): void

Loads .env files from a specific directory.

loader.loadFromDir("./config");

getAllVariableNames(): string[]

Gets all variable names from all loaded .env files (without loading into environment).

const names = loader.getAllVariableNames();
console.log(names); // ["API_KEY", "DATABASE_URL", ...]

getAllVariables(): Record<string, string>

Gets all environment variables as an object (decrypted). Note: This loads variables into the process environment first.

const env = loader.getAllVariables();
console.log(env.API_KEY);

resolveEnvPaths(dir: string): string[]

Computes the ordered list of env file paths that would be loaded.

const paths = loader.resolveEnvPaths(".");
console.log(paths); // [".env", ".env.local", ...]

JsSecretManager

Manages encryption and decryption of secrets.

JsSecretManagerGenerate(): JsSecretManager

Generates a new random encryption key pair.

const manager = dotenvage.JsSecretManagerGenerate();

JsSecretManagerNew(): JsSecretManager

Creates a SecretManager by loading the key from standard locations:

  1. DOTENVAGE_AGE_KEY environment variable
  2. AGE_KEY environment variable
  3. EKG_AGE_KEY environment variable
  4. OS keychain entry (service: dotenvage or DOTENVAGE_KEYCHAIN_SERVICE; account: AGE_KEY_NAME or {CARGO_PKG_NAME}/dotenvage)
  5. Key file at ~/.local/state/dotenvage/dotenvage.key

OS keychain lookup currently uses:

  • macOS: Keychain via security
  • Linux/Unix: Secret Service via secret-tool
  • Windows: lookup falls back to file/env sources (no keychain lookup yet); keygen --store os|both stores using cmdkey
const manager = dotenvage.JsSecretManagerNew();

publicKeyString(): string

Gets the public key as a string (starts with age1).

const publicKey = manager.publicKeyString();

encryptValue(plaintext: string): string

Encrypts a plaintext value.

const encrypted = manager.encryptValue("secret");

decryptValue(value: string): string

Decrypts a value if it's encrypted; otherwise returns it unchanged.

const decrypted = manager.decryptValue(encrypted);

isEncrypted(value: string): boolean

Checks if a value is in a recognized encrypted format.

const isEncrypted = manager.isEncrypted("ENC[AGE:b64:...]");

Utility Functions

shouldEncrypt(key: string): boolean

Checks if a key name should be encrypted based on auto-detection patterns.

dotenvage.shouldEncrypt("API_KEY"); // true
dotenvage.shouldEncrypt("PORT"); // false

CLI Usage

The package includes a dotenvage CLI command:

npx dotenvage list
npx dotenvage get API_KEY
npx dotenvage dump --export

See the main dotenvage README for full CLI documentation.

Next.js Integration

For Next.js applications, use the dedicated integration utilities:

// In next.config.mjs
import { loadEnv } from "@dotenvage/node/nextjs";
loadEnv();

Or use the pre-initialization loader to ensure environment variables are available for Edge Runtime (middleware):

{
  "scripts": {
    "dev": "node -r @dotenvage/node/nextjs/preinit node_modules/.bin/next dev"
  }
}

See nextjs/README.md for complete Next.js integration documentation.

TypeScript Examples

See the examples/ directory for TypeScript examples:

  • load-env.ts - Loading environment variables
  • encrypt-decrypt.ts - Encryption and decryption
  • app-config.ts - Type-safe application configuration
  • auto-encrypt.ts - Auto-detection patterns
  • nextjs-config.ts - Basic Next.js usage example

File Layering

dotenvage supports automatic file layering with precedence:

  1. .env - Base configuration
  2. .env.<ENV> - Environment-specific (e.g., .env.production)
  3. .env.<ENV>.<OS> - OS-specific (e.g., .env.production.linux)
  4. .env.<ENV>.<OS>.<ARCH> - Architecture-specific
  5. .env.<ENV>.<OS>.<ARCH>.<USER> - User-specific overrides
  6. .env.<ENV>.<OS>.<ARCH>.<USER>.<VARIANT> - Variant-specific (e.g., .env.production.linux.amd64.alice.docker)

Files are loaded in specificity order - more specific files override less specific ones. Dimension values can also be discovered from loaded files (e.g., NODE_ENV=production in .env causes .env.production to load).

Key Management

Setting the Encryption Key

Set one of these environment variables:

export DOTENVAGE_AGE_KEY="AGE-SECRET-KEY-1..."
export AGE_KEY="AGE-SECRET-KEY-1..."
export EKG_AGE_KEY="AGE-SECRET-KEY-1..."

Generating a Key

const manager = dotenvage.JsSecretManagerGenerate();
const publicKey = manager.publicKeyString();
console.log(`Public key: ${publicKey}`);
console.log(`Set: export DOTENVAGE_AGE_KEY="<private-key>"`);

Or use the CLI:

npx dotenvage keygen

Comparison with dotenv

Featuredotenv@dotenvage/node
Load .env files✅✅
Mutate process.env✅✅
Encrypt secrets❌✅
Commit to git safely❌✅
File layering❌✅
Auto-detection❌✅
Native performance❌✅ (Rust)

Requirements

  • Node.js >= 18.0.0
  • Valid age encryption key (set via environment variable or key file)

Building from Source

From the npm/ directory:

cd npm
npm install
npm run build

Or from the project root:

npm run npm:install
npm run npm:build

License

Copyright © 2026 DataRoad Inc, Delaware, USA, trading as Legra.

Licensed under either the MIT license or the Apache License, Version 2.0, at your option.

版本与发布记录

@dotenvage/node 共发布 46 个版本,首次发布于 2026/01/11;过去 12 个月发布了 46 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.8.25latest2026/11/13今天
0.8.242026/11/121 天前
0.8.232026/11/112 天前
0.8.222026/11/112 天前
0.8.212026/11/112 天前
0.8.202026/11/103 天前

关于 @dotenvage/node 的常见问题

@dotenvage/node 是什么?

Node.js bindings for dotenvage - Dotenv with age encryption

如何安装 @dotenvage/node?

在项目目录运行 npm install @dotenvage/node,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 dotenvage、dotenvage-next,可以用 npx dotenvage 直接运行。

@dotenvage/node 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dotenvage/node 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dotenvage/node 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dotenvage/node 可以商用吗?

@dotenvage/node 使用 MIT OR Apache-2.0 许可。可在 MIT、Apache-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dotenvage/node 的下载量有多少?

根据 npm 官方下载统计,@dotenvage/node 在 2026/10/13 – 2026/11/11 这 30 天内被下载 4,462 次,日均约 149 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dotenvage/node 还在维护吗?

最新版本 0.8.25 发布于 2026/11/13(今天);至今共发布 46 个版本,其中过去 12 个月发布了 46 个;首次发布于 2026/01/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。