Search code examples
node.jsenvironment-variablescpanelnext.jsproduction-environment

Environment variables in NodeJs using cPanel


So I'm using cPanel with Setup Node.js App plugin for a Next.js app. (don't asky why cPanel)

Everything is working as expected in development, except for environment variables in production, I set up them manually from the cPanel interface, I restarted/stopped the app, logging the process.env on the server and I don't see the env variables there (not to say when trying to call them they are undefined).

When doing res.json(JSON.stringify(process.env)); i get a bunch of variables except for the one I manually wrote in cPanel variables interface.

It is important for me to store these variables as secret key because they are API credentials.

Anyone know what I might have misconfigured or had this problem?


Solution

  • Never mind, found the answer, apparenlty was a Next.js misconfiguration. I had to add the following lines of code inside next.config.js in order to read env variables on build version.

    require('dotenv').config();
    
    module.exports = {
      env: {
        EMAIL_NAME: process.env.EMAIL_NAME,
        EMAIL_PASSWORD: process.env.EMAIL_PASSWORD,
        GETRESPONSE_API_KEY: process.env.GETRESPONSE_API_KEY
      }
    };
    

    Where EMAIL_NAME, EMAIL_PASSWORD, GETRESPONSE_API_KEY were the variables defined by me on cPanel interface