Env property undefined in deployed serverless function
See original GitHub issueDescribe 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:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
It was released on stable last night 👍
You heroes! Thanks @ijjk @timneutkens! 😄