sonamu.config.ts, Sonamu reads dotenv files from the API package root. Put
shared defaults in .env, environment-specific values in .env.<environment>, and machine-local
overrides in .env.local.
Loading Order
For the activeNODE_ENV, Sonamu applies sources in this order:
.env.env.${NODE_ENV}.env.local- Exported process environment variables
test, development,
staging, and production.
If the runtime NODE_ENV is not set, Sonamu checks NODE_ENV in .env.local, then .env, and
otherwise uses a default that depends on how Sonamu runs: production for sonamu build,
sonamu start, and servers started with createServer(); development for sonamu dev, tests,
and CLI tools such as sync and migrate. .env.${NODE_ENV} is read only after the environment
is chosen, so a NODE_ENV inside it is ignored for this decision.
Programmatic Environment API
Import the current environment reader and its return type from the root package:getSonamuEnvironment() does not read dotenv files; it only checks NODE_ENV in the given
environment object (process.env by default). resolveSonamuEnvironment(rootPath, fallback) picks
the environment from the runtime NODE_ENV, then .env.local and .env in rootPath, then
fallback, without modifying process.env.
SonamuEnvironment is "test" | "development" | "staging" | "production". An absent or empty
NODE_ENV returns "development". Any non-empty value outside these four environments throws an
error.
File Layout
.env file is optional when the current environment has its own file. One of .env or
.env.${NODE_ENV} must exist.
Common Values
.env
Environment Values
- Development
- Staging
- Production
- Test
.env.development
sonamu.config.ts
sonamu.config.ts can read process.env directly. Sonamu loads the current environment before it
imports the config file.
sonamu.config.ts
database.name or database.environments fields. Database names and
environment-specific connections now come from SONAMU_DB_* variables and projectName.
Conversion Tips
- Replace
DB_HOST,DB_PORT,DB_USER,DB_PASSWORD, andDATABASE_NAMEwithSONAMU_DB_HOST,SONAMU_DB_PORT,SONAMU_DB_USER,SONAMU_DB_PASSWORD, andSONAMU_DB_NAME. - Use
SONAMU_DB_READONLY_*for read-only replicas. Any missing read-only field uses the current environmentβs main DB value. - Use
SONAMU_DB_FIXTURE_*only for the test fixture database. - Use
.env.localfor machine-local overrides that should apply after the shared and environment-specific files.