npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

portable-env

v1.4.1

Published

Portable environment variables in the browser and server

Readme

portable-env

Read environment variables through the same function in Node.js and browser bundles. Node.js uses process.env; browsers use window.__ENV__.

npm install portable-env
import { env, requiredEnv } from 'portable-env';

env({ API_URL: 'https://api.example.com' });
console.log(requiredEnv('API_URL'));

env() returns the current environment object. env(values) copies string values into it, overwriting matching keys and preserving unrelated keys, then returns that same object. requiredEnv(name) throws if the value is missing or empty. Neither function reads files.

For a browser, you can supply values before the application bundle loads:

<script>
  window.__ENV__ = { API_URL: 'https://api.example.com' };
</script>
import { requiredEnv } from 'portable-env';

fetch(`${requiredEnv('API_URL')}/status`);

If window.__ENV__ is absent, env() creates it on first use. Only expose public configuration to browsers, never server credentials.

Loading a file in Node.js

Create a .env file containing API_URL=https://api.example.com, then:

import { requiredEnv } from 'portable-env';
import { loadEnv } from 'portable-env/node';

const { error } = loadEnv();
if (error) throw error;
console.log(requiredEnv('API_URL'));

loadEnv(path = '.env') reads one UTF-8 file synchronously. Relative paths resolve from the current working directory; absolute paths are accepted. File values override existing values. Success returns { parsed }; failure returns { error } with the original filesystem error code. Importing the package never loads a file.

Tests select their package's .env.test explicitly. When credentials may already come from the shell or CI, allow only a missing file:

import { loadEnv } from 'portable-env/node';

const { error } = loadEnv('/absolute/path/to/package/.env.test');
if (error && error.code !== 'ENOENT') throw error;

Load configuration before importing application modules that read it. For ESM, use a setup entry point that loads the file and then dynamically imports the app; static imports execute before the setup body.

The format supports KEY=value, blank lines, full-line # comments, surrounding single or double quotes, and LF or CRLF line endings. The first = separates the key and value; later assignments to the same key win. Lines without an assignment or with an empty key are ignored. Inline comments, multiline values, escape expansion, export prefixes and variable interpolation are not supported. This is not a full dotenv parser.

The root API works in browsers and Node.js 0.8 or newer. File loading is Node-only. CommonJS uses named properties, for example require('portable-env').env and require('portable-env/node').loadEnv.