
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
vite-plugin-envsubst
Advanced tools
Vite plugin that transforms environmental variables into a global statement that can be replaced with `envsubst` or Caddy's templating engine.
A Vite plugin that transforms import.meta.env references to use runtime environment variables via envsubst or Caddy's templating engine. Built for deploying SPAs in Docker containers where the environment isn't known at build time, to prevent having to build per environment.
When deploying SPAs in Docker, you often don't know your environment variables at build time. This is especially true when:
The typical solution is to build separate images per environment, which is inefficient, and quite frankly, annoying.
This plugin:
vite-env.d.ts file for environment variable declarationsimport.meta.env references to use a global objectindex.html that initializes environment variables with placeholder ENV variables.npm install vite-plugin-envsubst --save-dev
// vite.config.ts
import { defineConfig } from "vite";
import { envSubstPlugin } from "vite-plugin-envsubst";
export default defineConfig({
plugins: [
envSubstPlugin({
globalObject: "globalThis", // optional, defaults to globalThis
templateEngine: "envsubst", // optional, "envsubst" (default) or "caddy"
include: [/\\.[cm]?[jt]sx?$/], // optional, files to transform
exclude: [/node_modules/], // optional, files to skip
}),
],
});
Your environment variables must be declared in src/vite-env.d.ts:
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_API_URL: string;
readonly VITE_APP_TITLE: string;
readonly UNUSED_VARIABLE: string; // only variables prefixed with `envPrefix` (default VITE_) are transformed
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}
Then use them in your code as normal:
console.log(import.meta.env.VITE_API_URL);
envsubstWhen building with vite, this injects this script in your index.html, and transforms all import.meta.env.VITE_* variables into globalThis.env.VITE_* variables.
<script>
globalThis.env = globalThis.env || {};
globalThis.env.VITE_APP_TITLE = "${VITE_APP_TITLE}";
</script>
After building, in your docker entrypoint, make sure envsubst is called on the corresponding index.html. In nginx:alpine, this looks like:
FROM nginx:alpine
WORKDIR /usr/share/nginx/html
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
COPY nginx/envsubst-on-index.sh /docker-entrypoint.d/50-envsubst-on-index.sh
RUN chmod +x /docker-entrypoint.d/50-envsubst-on-index.sh
COPY --from=builder dist .
# envsubst-on-index.sh
#!/bin/sh
envsubst < /usr/share/nginx/html/index.html > /usr/share/nginx/html/index.html.tmp
mv /usr/share/nginx/html/index.html.tmp /usr/share/nginx/html/index.html
Note that envsubst is typically not included in hardened nginx images, such as chainguard's.
To use Caddy's templating engine instead of envsubst:
envSubstPlugin({
templateEngine: "caddy",
});
<script>
globalThis.env = globalThis.env || {};
globalThis.env.VITE_APP_TITLE = "{{env "VITE_APP_TITLE"}}";
</script>
vite-env.d.tsimport.meta.env.VAR = ...rolldown-stringenvPrefix configurationrolldown-string)MIT or APACHE 2.0
FAQs
Vite plugin that transforms environmental variables into a global statement that can be replaced with `envsubst` or Caddy's templating engine.
The npm package vite-plugin-envsubst receives a total of 702 weekly downloads. As such, vite-plugin-envsubst popularity was classified as not popular.
We found that vite-plugin-envsubst demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.