Configuration

Environment

Load environment variables from one or more .env files into process.env with AxilJS loadEnv and parseEnvFile.

1 min readUpdated DocumentationEdit this page
On this page5

Usage

TypeScript
TS
import { loadEnv } from '@axiljs/config'
loadEnv()                               // reads process.cwd()/.env
loadEnv({ path: '/custom/path/.env' })

Missing files are skipped silently.

.env format

Environment
ENV
1PORT=3000
2NODE_ENV=development
3JWT_SECRET=your-secret-minimum-32-characters
4DATABASE_URL=postgres://user:pass@localhost:5432/mydb
5GREETING="hello world"   # inline comments are allowed
6MULTILINE="line 1
7line 2"

Double-quoted values expand \n; single-quoted values are literal.

Multiple files

TypeScript
TS
loadEnv({ files: ['.env.local', '.env'] })

Precedence

OptionDefaultDescription
pathprocess.cwd()/.envSingle file to load (ignored when files is set)
filesnoneFiles to load in order, resolved from process.cwd()
overridefalseWhen true, file values overwrite variables already in process.env

Note

With the default override: false, a variable that is already set is never replaced — not by the system environment and not by an earlier file in files. List the most specific file first, or pass override: true to let later files win.

Parse without loading

TypeScript
TS
import { parseEnvFile } from '@axiljs/config'

const vars = parseEnvFile('A=1\nB="two"')   // { A: '1', B: 'two' }

Warning

Never commit .env to version control.

Was this page helpful?

Help improve the documentation

AxilJS is open source and documentation improvements are welcome.

AxilJS DocumentationMIT License · Built by SyntaxilitY