question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Env property undefined in deployed serverless function

See original GitHub issue

Describe the bug

I am connecting to my database in a serverless function. This works locally, but not once deployed. My database connection string is stored in a .env file. When deployed this string is undefined. The env file is committed and in the repo.

Github repo example - https://github.com/AshConnolly/next-env-issue

Here is my example .env file:

API_KEY='default-api-key'
CLIENT_KEY='default-client-key'
NEXT_PUBLIC_APP_LOCALE='en-us'
TEST='test'

When outputted into a page:

  <li>API_KEY='default-api-key' : {process.env.API_KEY}</li>
  <li>CLIENT_KEY='default-client-key' : {process.env.CLIENT_KEY}</li>
  <li>NEXT_PUBLIC_APP_LOCALE='en-us' : {process.env.NEXT_PUBLIC_APP_LOCALE}</li>
  <li>TEST='test' : {process.env.TEST}</li>

It shows only the value NEXT_PUBLIC_APP_LOCALE - as expected √.

However in a serverless function, it does not read my env variables correctly. This is what my serverless function looks like:

export default async (req, res) => {
  res.setHeader('Content-Type', 'application/json')
  res.statusCode = 200
  res.json({
    API_KEY: process.env.API_KEY,
    CLIENT_KEY: process.env.CLIENT_KEY,
    NEXT_PUBLIC_APP_LOCALE: process.env.NEXT_PUBLIC_APP_LOCALE,
    TEST: process.env.TEST,
  })
}

Expected behavior

Locally this shows correctly when i visit http://localhost:4000/api/test, outputting:

{
"API_KEY": "default-api-key",
"CLIENT_KEY": "default-client-key",
"NEXT_PUBLIC_APP_LOCALE": "en-us",
"TEST": "test"
}

Actual behavior when deployed

However when deployed it does not. it only shows the NEXT_PUBLIC_APP_LOCALE -

{
"NEXT_PUBLIC_APP_LOCALE": "en-us"
}

To Reproduce

Github repo - https://github.com/AshConnolly/next-env-issue Run locally, and deploy it to vercel to see the issue.

System information

Not applicable as its the deployment issue.

Why is this happening? Thanks! 👍

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
timneutkenscommented, May 28, 2020

It was released on stable last night 👍

1reaction
AshConnollycommented, May 28, 2020

You heroes! Thanks @ijjk @timneutkens! 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Environment variables undefined in serverless invoke test
However, when using serverless offline start , environment variables are set properly. Running this from my test file: console.log("process.env.
Read more >
aws lambda - serverless - Type Error: Cannot read property ...
The plugin doesn't support working with the role property. You have the following in your provider sections: role: arn-for-deployment-role'.
Read more >
Using AWS Lambda environment variables
Configuring environment variables · Open the Functions page of the Lambda console. · Choose a function. · Choose Configuration, then choose Environment variables....
Read more >
Serverless Environment Variables - A Comprehensive Guide
will deploy the new code to the fetchData Lambda function. Now, if we go to the fetchData Lambda function under the AWS Lambda...
Read more >
Environment Variables | Twilio
Any Environment Variables that have been set will be accessible in your Function as properties of the context object by name. For example,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found