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 🙏

© 2024 – Pkg Stats / Ryan Hefner

load-dotenv

v0.0.10

Published

Automatically load the closest .env file

Downloads

113

Readme

Load Dot ENV

Automatically find the closest .env file in any parent directory of the cwd, and then load those environment variables into process.env.

Why?

By default the dotenv package only looks in the current working directory of the Node.js process for a .env file. And if you use the path option, that path must be identical both when running locally and when running in production.

Monorepos are a common example when it may be convenient to have a .env file that is not in your cwd. load-dotenv allows you to have a single .env file used by every package, regardless of file structure.

load-dotenv provides a simple interface to load your environment variables, only requiring you to add a single import statement. That avoids the hassle of wanting to load your environment vars before any other code runs, but also having to place all the imports at the top of your file before any other code.

Install

Make sure you also install dotenv, which is a peer dependency of this package.

npm i dotenv load-dotenv     # With npm
pnpm i dotenv load-dotenv    # With pnpm
yarn add dotenv load-dotenv  # With yarn

Find and load the closest .env file

As early as possible in your application, import load-dotenv/load:

import 'load-dotenv/load'

Or in Common JS:

require('load-dotenv/load')

This will by default throw an error if it can't find a .env file in any parent directory. To not throw an error, import load-dotenv/load/optional instead.

Or load it yourself

import {findEnv} from 'load-dotenv'
import * as dotenv from 'dotenv'

const envFilePath = findEnv()
dotenv.config({path: envFilePath})

Or in Common JS:

const {findEnv} = require('load-dotenv')
const dotEnv = require('dotenv')

const envFilePath = findEnv()
dotEnv.config({path: envFilePath})

Or with a custom name:

const envFilePath = findEnv('.env.local')
dotenv.config({path: envFilePath})