vercel / serve

Static file serving and directory listing
https://npmjs.com/package/serve
MIT License
9.38k stars 691 forks source link

Environment variables are not recognized in a non-local environment #710

Open aj-concirrus opened 2 years ago

aj-concirrus commented 2 years ago

Hi there,

I have been having some issues using serve correctly. What I know is:

  1. In my local the environment variables are recognized after doing the build and running serve.
  2. In any non-local environment it is not able to consume such variables using serve.
  3. Initially we have been running the development server (npm start), it has no problem consuming the variables in each of the environments. Therefore, our servers do have the environment variables correctly configured and these can be consumed in a non-prod environment.

Any experience being unable to consume environment variables using serve?

pilare commented 2 years ago

I have the same issue. In our react application we are using some environmental variables and I cannot make them to be read while running the application with npm serve. However when runnning with webpack or vite development server there is no issue. Does serve allows to access process.env or is there any other solution for that?

wilsonehusin commented 1 year ago

For anyone finding themselves here — the process.env variable is evaluated at build time, i.e. when npm run build or equivalent happens. Make sure the variable is present at the time of building!