Generate Environment Runtime File

The generate-env command creates a runtime environment file at public/__env.js.

This is useful when your frontend reads public environment values at runtime instead of baking all values at build-time.

Command

pnpm exec leadcms generate-env

What it does

  • Loads variables from .env and .env.local.
  • Filters keys by NEXT_PUBLIC_ prefix.
  • Writes the result to public/__env.js in the form:
window.__env = {
  NEXT_PUBLIC_EXAMPLE: "value"
};

Typical usage

Run it before starting or building your app when runtime env injection is required:

pnpm exec leadcms pull
pnpm exec leadcms generate-env

In many projects, this is added to prebuild or deployment scripts.

Best practices

  • Keep secrets out of NEXT_PUBLIC_ variables.
  • Regenerate __env.js when public env values change.
  • Commit or ignore public/__env.js according to your deployment strategy.

Next steps