Next.js: .env variables behave different between local and Vercel

Created on 25 May 2020  路  8Comments  路  Source: vercel/next.js

Bug report

Describe the bug

Environment variables cannot be accessed in api routes when deployed on Vercel using the latest 9.4 environment variable feature.

However, when running next locally, then they do have access to the environment variables.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

See minimal reproduction sample: https://github.com/TheHolyWaffle/env-test

  1. Create .env file with "TEST=Hello World"
  2. Create an api route that echoes that env variable
  3. Deploy to Vercel

Expected behavior

The deployment on Vercel should have the same .env behaviour as when running nextjs locally using next dev.

  • Local: the api route correctly echoes "Hello World".
  • Vercel: the api route returns an internal server error. (Try https://env-test.theholywaffle.now.sh/api/print)

System information

  • Next.js 9.4.2
bug

Most helpful comment

Can confirm that 9.4.3-canary.1 seems to have fixed the issue: https://env-test-three.now.sh/api/print

All 8 comments

Are you deploying using the CLI instead of through git? Potentially you're using an older version of the CLI which does not upload .env

@timneutkens I'm using the GitHub integration of Vercel, see https://github.com/TheHolyWaffle/env-test/commit/ed4c3439113539ff9639bb9d27e52e6a5ac8a630

I've doublechecked that .env file is present on Vercel:
image

We'll look into it and apply a fix 馃憤

Can confirm that 9.4.3-canary.1 seems to have fixed the issue: https://env-test-three.now.sh/api/print

@TheHolyWaffle thanks for checking!

I have the same problem, should I update my version of next.js? If yes, what version?

@denispixi yes you should update to the latest stable version of Next.js (v9.4.4) to resolve this

@denispixi yes you should update to the latest stable version of Next.js (v9.4.4) to resolve this

I'll try. Thanks!

Was this page helpful?
0 / 5 - 0 ratings