motdotla / dotenv

Loads environment variables from .env for nodejs projects.
https://www.dotenvx.com
BSD 2-Clause "Simplified" License
18.86k stars 844 forks source link

Should dotenv recursively look for .env files in parent directories until it finds one? #251

Closed Jakobud closed 6 years ago

Jakobud commented 6 years ago

I was working on a project recently where I have a .env at the project root and using dotenv as usual. But there were some database migration files that were in a subdirectory that needed to be executed. Obviously I want to use the .env because it contains the database credentials.

In order to use dotenv here, I had to specify the path to the .env:

require('dotenv').config({path: '../.env'})

Now this works great. But an alternative solution to this would be to code in dotenv to recursively look for the .env in parent directories if it kind find one. I believe npm does the same thing, looking for a package.json file. It seems like the code for this would be very easy to implement.

Is this functionality that the developer has considered?

BTW, love <3 this package. I use it with, like every web project. It's the first package I install every time.

zenflow commented 6 years ago

@Jakobud I wanted this same kind of functionality, in my case because my .env files are in different relative locations in dev vs prod. See issue #238. We found a pretty simple one-liner pattern to accomplish this without any changes to dotenv.

Still, +1 for having it work this way out-of-the box.

maxbeatty commented 6 years ago

one-liner is the way to go if you want recursive lookup.

usually node db/migrate.js instead of cd db && node migrate.js should use the expected .env file. hard to say without knowing more about your specific setup

Jakobud commented 6 years ago

@maxbeatty Yeah, so I'm using Knex.js. The way it works is that you run it from the project root, but if your knexfile.js (configuration file) is in a subdirectory, which mine is, then it essentially changes the CWD to that directory instead of the project root. So then it's trying to find the .env in the subdirectory even though you executed it from the root directory. Make sense?

dandv commented 1 month ago

If dotenv looked for .env files in the parent dir(s), that would remove the need to use the 8-year old find-config package, which creates problems with modern TypeScript. This feature would also be in line with how ESLint and other tools merge configuration files from parent directories.

My use case is having one .env file in the project root, then running myscript.ts from a subdirectory, which won't find the .env file in that subirectory.

motdotla commented 1 month ago

@dandv have you tried using dotenvx's tooling to solve for this?

i'm moving a lot of new features there as dotenv is heavily depended on as is in the community. i'm hesitant to make these kind of significant changes here as they could have big side effects.

dandv commented 1 month ago

I can't find anything in the README or in the dotenvx docs about automatically looking for .env files in parent directories. Where should I look?

motdotla commented 1 month ago

true, there is not specifically. some users are combining dotenvx ls with dotenvx run to get the job done like this, but maybe we should make a default feature with a special flag.

would you mind opening a ticket on dotenvx's github issues for that?