# .env environment variables not working Express

**URL:** <https://community.auth0.com/t/env-environment-variables-not-working-express/74685>\
**Category:** Get Help\
**Tags:** environment, api, auth0\
**Created:** [December 12, 2021, 10:14pm UTC](https://community.auth0.com/t/env-environment-variables-not-working-express/74685 "2021-12-12T22:14:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![izzybolt95](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/izzybolt95/32/33697_2.png) [@izzybolt95](https://community.auth0.com/u/izzybolt95)\
**Post date:** [December 12, 2021, 10:14pm UTC](https://community.auth0.com/t/env-environment-variables-not-working-express/74685/1 "2021-12-12T22:14:00Z")

</div>

I am using the Express server as in this [Add Authentication to Existing Node.js/Express Apps with Auth0 -- Express and Authentication Series - YouTube](https://www.youtube.com/watch?v=w1zvS9-k7EU&t=0s) but getting an error whenever I use environment variables in my config.

```auto
const express = require("express");
const bodyParser = require("body-parser");
const { auth } = require("express-openid-connect");
require("dotenv").config();
const PORT = process.env.PORT || 3005;
const indexRouter = require("./routes/index");
const usersRouter = require("./routes/users");

const config = {
    authRequired: false,
    auth0Logout: true,
    secret: process.env.APP_SESSION_SECRET,
    baseURL: process.env.BASE_URL,
    clientID: process.env.CLIENT_ID,
    issuerBaseURL: process.env.ISSUER_BASE_URL
};

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// middleware, method returns middleware
// used in request
//can be handled in regards to payloads
//query strings and post type payloads that might contain json
//url encoded query strings. Extended data, so we can handled 
//nested data coming in query strings

//auth router attaches /login, /logout and /callback router to baseURL
app.use(auth(config));

app.use("/api/", indexRouter);
app.use("/api/users", usersRouter);

app.listen(PORT, () => {
    console.log(`server listening on port ${PORT}`)
});

```

not sure what I’m doing wrong!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 13, 2021, 9:39am UTC](https://community.auth0.com/t/env-environment-variables-not-working-express/74685/2 "2021-12-13T09:39:20Z")

</div>

Hey there!

Thanks for asking. Reaching out to @bendechrai (the video author) to see if he can help us on that front. Thank you!

---

<div class="post-metadata">

**Author:** ![bendechrai](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bendechrai/32/8024_2.png) [@bendechrai](https://community.auth0.com/u/bendechrai)\
**Post date:** [December 13, 2021, 9:59am UTC](https://community.auth0.com/t/env-environment-variables-not-working-express/74685/3 "2021-12-13T09:59:58Z")

</div>

Hi @izzybolt95!

A couple of questions to make sure you’re on track 🙂

Can you confirm where in the file system the `.env` file is located? It should be in the document root, is that right?

If so, have you restarted `node` since saving the changes to the `.env` file?

If yes, can you paste the contents of the `.env` file in here without the actual values? I don’t want you to share secrets and private information.

Also, if all of this is looking fine, can you confirm if the output of the following code, added after you define the `config` object, looks accurate? Feel free to share here if you like, but remember to sanitize and not share sensitive information…

```auto
console.log("App Session Secret = " + process.env.APP_SESSION_SECRET);
console.log("Base URL = " + process.env.BASE_URL);
console.log("Client ID = " + process.env.CLIENT_ID);
console.log("Issuer Base URL = " + process.env.ISSUER_BASE_URL);

```

Thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![izzybolt95](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/izzybolt95/32/33697_2.png) [@izzybolt95](https://community.auth0.com/u/izzybolt95)\
**Post date:** [December 13, 2021, 3:08pm UTC](https://community.auth0.com/t/env-environment-variables-not-working-express/74685/4 "2021-12-13T15:08:33Z")

</div>

Hi Ben

Thanks for getting back to me. 🙂

here’s my .env code, in the root of my project

```auto
APP_SESSION_SECRET:'some long string'
BASE_URL:'http://localhost:3005'
CLIENT_ID:'xxxx'
ISSUER_BASE_URL:'https://whatever'

```

The outputs of the console.logs are strangely all undefined:

```auto
App Session Secret = undefined
Base URL = undefined
Client ID = undefined
Issuer Base URL = undefined

```

in my package.json file I have `"dotenv": "^10.0.0"` installed

Thanks,  
Izzy

---

<div class="post-metadata">

**Author:** ![izzybolt95](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/izzybolt95/32/33697_2.png) [@izzybolt95](https://community.auth0.com/u/izzybolt95)\
**Post date:** [December 13, 2021, 3:13pm UTC](https://community.auth0.com/t/env-environment-variables-not-working-express/74685/5 "2021-12-13T15:13:57Z")

</div>

I just realised my problem 😶 an error in my .env file… No it’s working again! Thanks for your help!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 15, 2021, 1:47pm UTC](https://community.auth0.com/t/env-environment-variables-not-working-express/74685/6 "2021-12-15T13:47:29Z")

</div>

No worries! We’ve all been there!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 22, 2021, 3:07pm UTC](https://community.auth0.com/t/env-environment-variables-not-working-express/74685/7 "2021-12-22T15:07:19Z")

</div>


