Search code examples
environment-variablesvitepnpm

Set vite env-varible value in the pnpm script


I tried to set varible right in to the pnpm script:

"build": BUILD_PLATFORM=web vite build

But getting an error:

'BUILD_PLATFORM' is not recognized as an internal or external command operable program or batch file

I need to set value for environment variable in script call

.env file approach seems to be incorrect, because every time I want to call one of my scripts, I should change varible value in .env file accordingly


Solution

  • I found my solution in pre-launching module that modifies the .env file before executing current script

    In my case it looks like this:

    "build:desktop": "tsx prebuild-setup/desktop.ts && vite build",
    "build:web": "tsx prebuild-setup/web.ts && vite build",
    

    desktop.ts & web.ts are executing before vite build and make changes to .env file, so at build time, code will use proper variable values, no need to change .env file manualy every time before calling scripts

    note: tsx - package for running TypeScript