
Research
2025 Report: Destructive Malware in Open Source Packages
Destructive malware is rising across open source registries, using delays and kill switches to wipe code, break builds, and disrupt CI/CD.
dotenv-safe
Advanced tools
Identical to dotenv, but ensures that all needed environment variables are defined after reading from .env.
The names of the needed variables are read from .env.example, which should be commited along with your project.
dotenv-safe only checks if all the needed variable names exist in process.env after initialising. It does not assume anything about the presence, format or validity of the values.
npm install dotenv-safe
pnpm install dotenv-safe
yarn add dotenv-safe
# .env.example, committed to repo
SECRET=
TOKEN=
KEY=
# .env, private
SECRET=topsecret
TOKEN=
// index.js
require('dotenv-safe').config();
Or, if you are using ES modules:
// index.mjs
import { config } from 'dotenv-safe';
config();
Since the provided .env file does not contain all the variables defined in
.env.example, an exception is thrown:
MissingEnvVarsError: The following variables were defined in .env.example but are not present in the environment:
TOKEN, KEY
Make sure to add them to .env or directly to the environment.
If you expect any of these variables to be empty, you can use the allowEmptyValues option:
require('dotenv-safe').config({
allowEmptyValues: true
});
Not all the variables have to be defined in .env; they can be supplied externally.
For example, the following would work:
$ TOKEN=abc KEY=xyz node index.js
Requiring and loading is identical:
require('dotenv-safe').config();
This will load environment variables from .env as usual, but will also read any variables defined in .env.example.
If any variables are already defined in the environment before reading from .env, they will not be overwritten.
If any variables are missing from the environment, a MissingEnvVarsError will be thrown, which lists the missing variables.
Otherwise, returns an object with the following format:
{
parsed: { SECRET: 'topsecret', TOKEN: '' }, // parsed representation of .env
required: { SECRET: 'topsecret', TOKEN: 'external' } /* key-value pairs required by .env.example
and defined by environment */
}
If all the required variables were successfully read but an error was thrown when trying to read the .env file, the error will be included in the result object under the error key.
dotenv-safe compares the actual environment after loading .env (if any) with the example file, so it will work correctly if environment variables are missing in .env but provided through other means such as a shell script.
You can use the --require (-r) command line option to preload dotenv-safe.
By doing this, you do not need to require and load dotenv in your application code.
This is the preferred approach when using import instead of require.
$ node -r dotenv-safe/config your_script.js
See the dotenv README for more information.
It can be useful to depend on a different set of example variables when running in a CI environment.
This can be done by checking if the CI environment variable is defined, which is supported by virtually all CI solutions.
For example:
require('dotenv-safe').config({
example: process.env.CI ? '.env.ci.example' : '.env.example'
});
Same options and methods supported by dotenv, in addition to the options below:
require('dotenv-safe').config({
allowEmptyValues: true,
example: './.my-env-example-filename'
});
Starting from version 9.0.0, dotenv is a peer dependency of dotenv-safe. This means that the actual version of dotenv used defaults to the latest available at install time, or whatever is specified by your application.
allowEmptyValuesIf a variable is defined in the example file and has an empty value in the environment, enabling this option will not throw an error after loading.
Defaults to false.
examplePath to example environment file.
Defaults to .env.example.
I regularly use apps that depend on .env files but don't validate if all the necessary variables have been defined correctly.
Instead of having to document and validate this manually, I prefer to commit a self-documenting .env.example file that may have placeholder or example values filled in. This can be used as a template or starting point for an actual .env file.
The dotenv package is a simpler version of dotenv-safe. It loads environment variables from a .env file into process.env but does not include validation of required variables. It is useful for basic use cases where validation is not necessary.
The env-cmd package allows you to specify environment variables in a JSON or .env file and load them into your application. It also supports multiple environment files for different environments (e.g., development, production). However, it does not provide the same level of validation as dotenv-safe.
The envalid package is a more advanced alternative that not only loads environment variables but also validates and sanitizes them. It provides a more robust solution for ensuring that environment variables are correctly defined and of the correct type. It is more feature-rich compared to dotenv-safe but may require more setup.
FAQs
Load environment variables from .env and ensure they are defined
The npm package dotenv-safe receives a total of 68,501 weekly downloads. As such, dotenv-safe popularity was classified as popular.
We found that dotenv-safe demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
Destructive malware is rising across open source registries, using delays and kill switches to wipe code, break builds, and disrupt CI/CD.

Security News
Socket CTO Ahmad Nassri shares practical AI coding techniques, tools, and team workflows, plus what still feels noisy and why shipping remains human-led.

Research
/Security News
A five-month operation turned 27 npm packages into durable hosting for browser-run lures that mimic document-sharing portals and Microsoft sign-in, targeting 25 organizations across manufacturing, industrial automation, plastics, and healthcare for credential theft.