Search code examples
next.jsenvironment-variablesmailchimp

environmental variable value not being processed


I am still learning about env. I'm not sure why it's not working. I have two files in my root project folder. '.env.local' and '.env'. And, the values for .env aren't being processed. However, .env.local is working fine. I believe I followed the same format.

For the .env file, if I just write them on the page and expose them, then they work just fine.

What am I missing here, this is on a NEXTJS app. In vercel I just added Environmental variable to their built in mechanism.

In my code I wrote this to get the value.

env local file

NEXT_PUBLIC_GOOGLE=xxxxxxxxxx

_app.js

my env file MAILCHIMP_PUBLIC_USERNAME=xxxxx MAILCHIMP_PUBLIC_DOMAIN=xxxx MAILCHIMP_PUBLIC_POST=xxxxxxxxxxxxxxxxxxxx& MAILCHIMP_PUBLIC_ID=xxxxxxxx

Solution

  • I think .env.local is overriding the other files. Also remember to add NEXT_PUBLIC_ to the variables. After editing the .env file you should stop and restart the development server. In your case you should do the following

    NEXT_PUBLIC_MAILCHIMP_PUBLIC_USERNAME=xxxxx
    NEXT_PUBLIC_MAILCHIMP_PUBLIC_DOMAIN=xxxx
    NEXT_PUBLIC_MAILCHIMP_PUBLIC_POST=xxxxxxxxxxxxxxxxxxxx
    NEXT_PUBLIC_MAILCHIMP_PUBLIC_ID=xxxxxxxx