-1

The problem is simple, but i dont know if the think i am doing is wrong, i will try to explain it and if some knows how to solve it wold be cool and i will thank you so much.

  1. remember that this app is made whit NextJS

  2. DotEnv variables are in a .env file that is in the root folder of my NextJs app.

  3. I have this data base connection where i am using DotEnv variables:

** UPDATE **

i am not destructuring more the data according to the NextJs documentation, and now my .env is .env.local and still not working, if someone knows why it is not working you are free to comment.

       const db = mysql({  
            config: {    
                host: process.env.MYSQL_HOST ,    
                port: process.env.MYSQL_PORT , 
                database: process.env.MYSQL_DATABASE , 
                user: process.env.MYSQL_USER ,  
                password: process.env.MYSQL_PASSWORD 
              }
       });  
       

by this way it is supposed to connect but it give me an access error to the database, but when i place the data like this without DotEnv.

const db = mysql({  
          config: {    
               host: "127.0.0.1" ,    
               port: 3306 , 
               database: "mydatabase" , 
               user: "myuser" ,  
               password: "123" 
         }
    });

the connection works fine. may be its the way i am declarating my .env file ore somthing, if you know what am I doing wrong I would be glad to know. thaks for reading.

  • Do you have any extra env vars setup in `next.config.js`? Where is the database code located and where are you trying to use it from? Could you provide a [mre]? – juliomalves Oct 05 '21 at 08:30
  • I have the database on the root folder in other folder called database, i was wondering if there is a problem whit that so i change the folder from places and nothing change, i set in pages folder. About the next.config.js file i have no variables should i have config somthing for DotEnv? – Pablo Cabrera Oct 05 '21 at 09:22
  • here is the github repo of how i have my project if you want to give it a try, just remember that i am less than a junior programer so don't expect too much haha, https://github.com/DiPa71/portfolio-web-app if this is no the minimal reproducible example that you mean, I am sorry i am new asking questions here. – Pablo Cabrera Oct 05 '21 at 10:05

1 Answers1

0

I've fallen into the same trap; Next handles environmental variables that are stored on .env.local files, rather than .env; So to retrieve them, you'll simply need to rename your file

Docs: https://nextjs.org/docs/basic-features/environment-variables

Edit

Found the gotcha - From the docs:

Note: In order to keep server-only secrets safe, Next.js replaces process.env.* with the correct values at build time. This means that process.env is not a standard JavaScript object, so you’re not able to use object destructuring. Environment variables must be referenced as e.g. process.env.NEXT_PUBLIC_PUBLISHABLE_KEY, not const { NEXT_PUBLIC_PUBLISHABLE_KEY } = process.env.

So aside from renaming your file to .env.local, make sure to also call your variables as process.env.VAR_NAME instead of destructuring

ale917k
  • 1,494
  • 7
  • 18
  • 37
  • i change the name of the file and use the process.env.variable, like they say on the documentation and still not working, it just work wen i place the data on the mysql config. is ther an other file for setting or somthing else that i have to do? – Pablo Cabrera Oct 04 '21 at 18:16
  • Have you restarted your server? – ale917k Oct 04 '21 at 18:20
  • yes i run the ctrl + c and yarn dev to restart the server and it doesnt work. – Pablo Cabrera Oct 04 '21 at 18:24
  • See the edit I made on the answer, wrote it there so it's cleaner – ale917k Oct 04 '21 at 18:31
  • yes as I tell you in the first comment I change the way i call the variables to be like in the documentation but it still not working, i have read in other post that maybe its beacouse i am using normal dotenv insted of dotenv-webpack, let me try that but it seems like it's not going to work. – Pablo Cabrera Oct 04 '21 at 18:35
  • You shouldn't have any extra library, remove them all. Next does that out of the box – ale917k Oct 04 '21 at 19:58
  • no it still not working, i don't get it why even if i remove or add both libraries doesn't work – Pablo Cabrera Oct 04 '21 at 20:42