toddbluhm / env-cmd

Setting environment variables from a file
https://www.npmjs.com/package/env-cmd
MIT License
1.73k stars 65 forks source link

Variables are undefined when build in production with Vitejs #347

Open jdinartejesus opened 2 years ago

jdinartejesus commented 2 years ago

I'm currently using Vuejs with Vitejs, but when I'm running the build in production, the variables are undefined. Vitejs it's like ReactCreateApp, using the REACTAPP prefix, but instead we need to use VITE_ prefix, and even when applying this still doesn't work.

.env-cmdrc.js

module.exports = {
  "development": {
     "VITE_APP_DB_ENV": "development",
     "VITE_APP_BUILD_ENV": "development",
     "VITE_HASURA_ENGINE_URL": "URL"
     ...
  },
  "staging": {
     "VITE_APP_DB_ENV": "staging",
     "VITE_APP_BUILD_ENV": "production",
     "VITE_HASURA_ENGINE_URL": "URL"
     ...
  },
  "production": {
     "VITE_APP_DB_ENV": "production",
     "VITE_APP_BUILD_ENV": "production",
     "VITE_HASURA_ENGINE_URL": "URL"
     ...
  }
}

package.json:

"scripts": {
  "start": "env-cmd -e ${APP_ENV:=production} npm run gql:generate && vite build",
}
nichita-pasecinic commented 2 years ago

@jdinartejesus did you try to pass just the production for the -e ? or switch with places the npm run gql:generate with vite build ? I've worked today with the env-cmd and vitejs and did not have any problems. Here is a simple example using both tools

Edit: seems like the env-cmd is setting the env variables for the first command, to run multiple commands see this docs