env-cmd icon indicating copy to clipboard operation
env-cmd copied to clipboard

Variables are undefined when build in production with Vitejs

Open jdinartejesus opened this issue 2 years ago • 1 comments

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 REACT_APP_ 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",
}

jdinartejesus avatar Mar 08 '22 20:03 jdinartejesus

@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

nichita-pasecinic avatar Apr 05 '22 17:04 nichita-pasecinic