Node Environment Run
node-env-run is a command-line utility designed to simplify the process of loading environment variables from `.env` files and executing Node.js scripts or other commands with those variables pre-loaded. It leverages the popular `dotenv` library under the hood for parsing `.env` files. The package is currently at version 4.0.2 and appears to be actively maintained; while an explicit release cadence isn't stated in the provided documentation, updates often follow the underlying `dotenv` library or address bug fixes. Its key differentiators include flexible command execution (e.g., with `nodemon`, `python`), the ability to specify custom `.env` file paths, and options to override existing environment variables, making it a robust tool for local development setups.
Common errors
-
nodenv: command not found
cause The `nodenv` executable is not available in the system's PATH, either because `node-env-run` was not installed globally, or `npm run` isn't used for a local installation.fixIf installed locally (`--save-dev`), run it via `npm run <script-name>` (e.g., `npm run dev` if `dev: "nodenv ."` is in `package.json`). If desired globally, install with `npm install -g node-env-run`. Alternatively, use `npx node-env-run`. -
TypeError: Cannot read properties of undefined (reading 'SOME_ENV_VAR')` or similar errors indicating missing environment variables at runtime.
cause The `.env` file either wasn't found, was misconfigured, or the environment variable was not correctly defined within it.fixVerify the `.env` file exists at the expected path (defaults to `./.env` or the path specified by `-E`). Ensure variables are defined in `KEY=VALUE` format. Check for typos in both the `.env` file and the code accessing `process.env.KEY`. -
(node:...) Warning: Accessing non-existent property 'NODE_ENV' of module exports inside the CJS wrapper of an ESM module will break in a future version.
cause This warning can occur when using `node-env-run` in projects mixing CommonJS and ES Modules, where Node.js's interop layer generates a warning due to a dependency's module resolution. It's usually not a direct `node-env-run` issue.fixWhile often harmless, review your project's module setup, particularly `package.json`'s `type` field and how dependencies are loaded. This warning is often transient and not directly caused by `node-env-run` itself.
Warnings
- gotcha Overriding existing environment variables can lead to unexpected behavior if not handled carefully. By default, `node-env-run` will *not* override existing process environment variables.
- gotcha Committing `.env` files directly into version control systems (like Git) is a common security mistake, potentially exposing sensitive information (API keys, database credentials) to unauthorized parties.
- gotcha Incorrectly specifying the path to the `.env` file will result in environment variables not being loaded, leading to runtime errors where applications expect certain configurations.
- breaking Older versions of `node-env-run` might have different default behaviors or argument parsing compared to the current v4.x. While the provided README doesn't explicitly detail breaking changes for v4, significant version bumps often introduce them, especially due to its dependency on `dotenv` which also has major versions.
Install
-
npm install node-env-run -
yarn add node-env-run -
pnpm add node-env-run
Imports
- nodenv (CLI Executable)
import { nodenv } from 'node-env-run'nodenv ./my-script.js
- node-env-run (npx usage)
npx nodenv ./my-script.js
npx node-env-run ./my-script.js
- TypeScript Types
import { EnvRunOptions } from 'node-env-run';import type { EnvRunOptions } from 'node-env-run';
Quickstart
// package.json
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"start": "nodenv ."
}
}
// .env
HELLO_MESSAGE="Hello from .env!"
PORT=3000
// index.js (main script)
console.log(`Loading application on port ${process.env.PORT ?? 'unknown'}.`);
console.log(process.env.HELLO_MESSAGE ?? 'No message loaded.');
// To run:
// 1. npm install node-env-run --save-dev
// 2. npm run start