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

dotenv-finder

v1.0.0

Published

dotenv-finder is a simple function that traverses the process.mainModule.paths attempting to locate a `.env` file.

Readme

dotenv-finder

dotenv-finder is a simple function that traverses the process.mainModule.paths attempting to locate a .env file.

Works best with dotenv by motdotla.

Why do I need this helper function?

Maybe you don’t? You probably don’t. In fact, I probably don’t… but here we are. With a .env file in your project’s root directory and the ubiquitous dotenv module you’re probably all set.

When set-up correctly, you’re already ready to node app.js or npm start from your project’s root directory… but what if you want to node foo.js in some other my-project/bar/ directory?

dotenv uses process.cwd() to get the expected root directory to search for the .env file. When in a directory other than project root (and therefore without your .env file) you won’t be able to load and use your .env variables (easily, nor dynamically).

# let's assume you have my-project/.env but are working in my-project/lib/
node foo.js
# won’t load your .env variables

dotenv has the ability to specify a path other than your current working directory with: .config({ path: ‘/foo/bar/.env’ }) which overrides the process.cwd() default. This, however, creates three challenges:

  1. Requires you to specify the absolute path to your .env file (which can be long and subject to typos and copypasta errors).
  2. Requires you to remember which project/path you’re in (which if you’re switching projects a lot is no easy task).
  3. Requires you to manually update this path if your project folder moves, etc.

I want the best of both worlds:

  1. Dynamically know which path and project I’m in
  2. Allow me to call node foo.js from any project subdirectory

Enter dotenv-finder

Install

# npm 5.0.0+ saves be default, otherwise add ` --save`
npm install dotenv-finder

Usage

This helper function assumes you already have a .env file in the root directory of your project, as well as dotenv already set-up and working.

# my-project/.env
FOO="bar"

BEFORE you require and configure dotenv you need to require dotenv-finder (also, we need to configure dotenv with our helper function findDotEnvFile()).

require(‘dotenv-finder’)
require(‘dotenv’).config({ path: findDotEnvFile() })

That’s it! You’re all set.

FAQ

Why?

See above. But mostly because I could. Well… I couldn’t, but I wanted to test and learn.

How does it work?

findDotEnvFile() looks in all the same paths as node_modules. It gets the paths list from process.mainModule.paths and tests each path for a .env file. It then returns the space-escaped absolute path to the .env file.

What about multiple .env files?

That's a great question... and I'm not sure. I haven't tested this on multiple .env files as I have no use for that setup. I assume the helper function would return the "left-most" .env file it could find... in other words, a .env file in /Users/foo/.env would likely return if found even if you wanted /Users/foo/my-project/.env -- use at your own risk!