How to access .env variables using dotenv

I have followed the instructions in here:

  1. Dotenv is installed
  2. There’s a route extension called dotenv.js that has the line require('dotenv').config()
  3. Created a .env file in the root folder of the project with two key value pairs

I can’t seem to get the value’s out. I tried in two ways:

  1. Setting a value and opening the server action in the browser

    There’s nothing there…

  2. On a page using EJS image →it shows empty

Am I doing it wrong?
@patrick @JonL

You need to insert it as server binding in the EJS page.

<%=_('$_ENV.APP_ID',locals)%>

Thanks @patrick, still doesn’t seem to work.
Copied your code:

Shows nothing on page:

.env contents and place in folder:


(I’m using Docker if it matters)

PS
I also tried to get it to print using a route extension:

Goal is to be able to use them in server actions though.

The dotenv module is already installed and used by server connect, it get only used in development used, when you want to use it in production then edit the file lib/server.js.

Sorry, I don’t understand. I’m in development right now (local development docker server). But I can’t seem to get the value out of the .env file

Did you get this to work ?

I’ve got a .env file in the root folder, but values are not read and not visible if i log out node_env

@namakemono I gave up on getting the dotenv to work.

I am using docker and solved it like this:

  1. Make a .env file that has the variables image image

  2. Add it into the docker-compose

Note: you’ll probably want to do this for multiple targets. I have different variables for the dev and production targets.

1 Like

I’m using Wappler’s standard solution without major issue.

So just dotenv in package.json and .env in root ?

Have to test with a fresh project and see if it works

Nothing else.

dotenv is already required at the beginning of server init.

image

Yeah, already tried removing the if statement and running it regardless of env.

Also put console.log in if statement to check if running but .env does not add variables

Really weird!

Is this an issue, perhaps, of the Export configuration?
Or is it a path/ issue?

Using docker env_file works for me, dotenv doesn’t.

1 Like

This is now available in Wappler 4.9.0

This topic was automatically closed after 2 days. New replies are no longer allowed.