adonisjs / env

Framework agnostic environment variables parser and validator
https://docs.adonisjs.com/guides/environment-variables
MIT License
37 stars 10 forks source link

- Missing environment variable "DB_DATABASE" while creating a new app #40

Closed L4znet closed 6 months ago

L4znet commented 6 months ago

Package version

6.8.0

Describe the bug

Hey everyone!

I'm not sure if this can be considered as an issue, but every time I try to create a new app, I get an error in my terminal saying that an environment variable is missing.

So, yeah, I just need to set the variable, but in my opinion, it can be confusing for a beginner.

The entire message :

EnvValidationException: Validation failed for one or more environment variables

  • Missing environment variable "DB_DATABASE"

    at start/env.ts:14 9| | 10| */ 11| 12| import { Env } from '@adonisjs/core/env' 13| ❯ 14| export default await Env.create(new URL('../', import.meta.url), { 15| NODE_ENV: Env.schema.enum(['development', 'production', 'test'] as const), 16| PORT: Env.schema.number(), 17| APP_KEY: Env.schema.string(), 18| HOST: Env.schema.string({ format: 'host' }), 19| LOG_LEVEL: Env.schema.string(),

    ⁃ EnvValidator node_modules/@adonisjs/env/src/validator.ts:27 ⁃ Function.rules node_modules/@adonisjs/env/src/env.ts:83 ⁃ Function.create node_modules/@adonisjs/env/src/env.ts:53

Have a good day

Reproduction repo

No response

thetutlage commented 6 months ago

What do you think could be the fix?

axelvaindal commented 6 months ago

@L4znet Just had the same while pulling a branch locally from one of my developers and wondered a bit about the real problem, until I realized I did not create the .env file.

@thetutlage Not sure it's clearer, but would a simple mention like this do the trick ?

EnvValidationException: Validation failed for one or more environment variables

Missing environment variable "DB_DATABASE"

Did you create `.env` file and corresponding variables ?  

Perhaps with a link to the env documentation ? I think this is how Next.js CLI handle such cases if I recall correctly

thetutlage commented 6 months ago

There could be many cases because of which an environment variable is missing.

I think the issue is quite straight forward and does not need additional tooling or context

L4znet commented 6 months ago

When you start the project, all credentials are not defined by default, that pretty logic..

But, if we look at the behavior of Laravel (for example) you can create a new app and not define any credentials in env vars, that's because Laravel define by default the database name as "Laravel" to prevent the error

In my opinion, for a beginner that wanna test if all works that's more friendly to say nothing and just send a error if he try to make a database request (because the default credentials are not correct..)

otherwise, why not explain that behavior in the doc by saying something like "Hey, just before you launch your app please be sure to setup the database credentials"

or, another solution is to give the possibility to fill the database name directly from the cli..