Skip to content

How to access environment variables in config.mts? #3333

Closed Answered by onurusluca
onurusluca asked this question in Q&A
Discussion options

You must be logged in to vote

After a lot of research, I got it working. I don't know if this is the best way, but hey it works:

  1. Install cross-env: pnpm add cross-env
  2. Add package.json scripts:
    "build:prod": "cross-env ENVIRONMENT=production vitepress build",
    "build:stg": "cross-env ENVIRONMENT=staging vitepress build",
    "build:dev": "cross-env ENVIRONMENT=development vitepress build",
  1. In config.mts, now we can use process.env.ENVIRONMENT
const sourceUrl =
  process.env.ENVIRONMENT=== 'production'
    ? 'https://prd-script.ts'
    : process.env.ENVIRONMENT=== 'staging'
    ? 'https://stg-script.ts'
    : process.env.ENVIRONMENT=== 'development'
    ? 'https://dev-script.ts'
    : 'http://localhost:5173/…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Karlstens
Comment options

@weizwz
Comment options

Answer selected by onurusluca
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants