dotenv-finder v1.0.0
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!
2 years ago