Use .conf configuration files and generate a TypeScript file.
With NPM:
npm install betterconf
With PNPM:
pnpm install betterconf
With Bun:
bun install betterconf
betterconf support .conf file with the Hocon specs. Here a short sample with dev and prod conf:
# config.dev.conf
{
env = ${NODE_ENV} # development
database {
provider = "sqlite"
url = "file:./dev.db"
}
}
# config.prod.conf
{
env = ${NODE_ENV} # production
database {
provider = "postgres"
url = "postgresql://postgres:postgres@localhost:5432/postgres"
}
}
Add betterconf to your package's scripts (Next.js example here):
"scripts": {
"dev": "betterconf run -r='config.dev.conf' -w='src/config.ts' -- next dev",
"build": "betterconf run -r='config.prod.conf' -w='src/config.ts' -- next build",
...
}
And use it anywhere from your code source:
import { config } from "./config"
console.log(config.env) // "development"
console.log(config.database.provider) // "sqlite"
console.log(config.database.url) // "file:./dev.db"
© Christophe Ribeiro under MIT License.