svelte-environment-variables

raw JSON →
1.0.4 verified Fri May 01 auth: no javascript maintenance

A tiny helper (v1.0.4, no recent updates) for Svelte/Sapper apps that replaces environment variables with a configurable prefix (default SVELTE_APP_) at build time using Rollup's @rollup/plugin-replace or Webpack's DefinePlugin. Unlike Vite's built-in import.meta.env or dotenv, this package is Rollup/Webpack-only and designed specifically for Svelte/Sapper. No TypeScript types are included. Last published in 2020, it is effectively in maintenance mode.

error Error: Cannot find module 'svelte-environment-variables'
cause Package not installed or not in node_modules.
fix
Run 'npm install svelte-environment-variables'
error Uncaught ReferenceError: process is not defined
cause process.env.X not replaced; likely missing replace plugin or configuration.
fix
Ensure @rollup/plugin-replace is configured with ...includeEnv() and that the variable has the correct prefix.
error TypeError: includeEnv is not a function
cause Using CommonJS require but package default export is not a function in that context.
fix
Use 'const includeEnv = require('svelte-environment-variables').default' or switch to ES6 import.
gotcha Only environment variables with the configured prefix are replaced; others remain as literal 'process.env.X' in the bundle.
fix Ensure all client-side env vars use the prefix (default 'SVELTE_APP_').
gotcha If using with env-cmd, the .env file must be loaded before the build script; otherwise variables may not be present.
fix Wrap build script with env-cmd: 'env-cmd npm run dev'
gotcha With @rollup/plugin-replace v3+, you must set 'preventAssignment: true' to avoid deprecation warnings.
fix Add preventAssignment: true to replace options.
deprecated Package has not been updated since 2020; considered in maintenance mode. No plans to support TypeScript, Vite, or SvelteKit.
fix Migrate to SvelteKit's $env module or Vite's import.meta.env for newer projects.
npm install svelte-environment-variables
yarn add svelte-environment-variables
pnpm add svelte-environment-variables

Configures Rollup to replace environment variables prefixed with SVELTE_APP_ at build time.

// install: npm install svelte-environment-variables @rollup/plugin-replace
// .env file: SVELTE_APP_API_URL="https://api.example.com"
// rollup.config.js
import replace from '@rollup/plugin-replace';
import includeEnv from 'svelte-environment-variables';

export default {
  client: {
    plugins: [
      replace({
        ...includeEnv({ filterPrefix: 'SVELTE_APP_', targetPrefix: 'process.env.' }),
        preventAssignment: true,
      }),
    ],
  },
};

// In Svelte component:
console.log(process.env.SVELTE_APP_API_URL); // outputs 'https://api.example.com'