Why .env file is ignored in production?

@Patrick, why .env file is ignored in production?

// lib/server.js
if (process.env.NODE_ENV !== 'production') {
    require('dotenv').config();
}

Wappler 6.7.0
NodeJS

P.S.: .env file can be added to .dockerignore and other Wappler upload ignore list

@Patrick why did you botch env implementation?

Checked an old project. First commit on 8th March 2021 has this code.
This is not new. Has always been like this.
And I haven't seen any problem ever regarding ENV variables because of this condition.

1 Like

This is only a problem when you're using a .env file to define environment variables. Variables defined in the Wappler editor or injected by Docker/Caprover/etc. are not affected.

It's standard practice to use a .env file in production (before Docker and before Wappler editor added environment variables), so the condition doesn't make sense

I haven't had a use case like so yet.
Can you share an example of how it would be used before?

For example in the PHP framework Laravel. You would upload your files through FTP to your shared hosting and create a .env file to contain your environment variables, and the PHP script would load them

In Wappler NodeJS I ran into this issue when using PM2 for deployment, it wasn't reading the .env file

I haven't used Laravel or PM2.
If I ask more questions, this will become a tutorial. :laughing:
Lets hope Patrick has some workaround for your use case.

As a last resort, I mess with core Wappler files like this and Git helps with updating them without losing my changes.

As for the environment variables, dotenv is really only meant for local development target only. You should just define your environment variables in the server connection options per target.

See:

Btw the usage and definition of environment variables, also per target, is described in:

So you don't actually need to use any .env file ...

You say dotenv is only for development like it's a best practice, yet you store environment variables in a JSON file not excluded in .gitignore, against best practices... :upside_down_face:
That dotenv condition to only run in development doesn't make sense to people like me, JonL, tbvgl and similar. The only possible problem here is if somewhere along Wappler life you forgot to ignore .env files from being uploaded in deployments, so if you were to remove that condition and people had a .env file uploaded it could mess things up.

I'm not using Wappler targets because of the following issues or feature requests:
1. Lack of Docker image push deployment method
2. Lack of Git push deployment method
3. Slow SFTP deployment compared to rsync

You make it so difficult for me :sob:

1 Like