Advanced
dotenvx.config(convention: 'nextjs')
Use convention in node.js code.
Set a convention when using dotenvx.config()
. This allows you to use the same file loading order as the CLI without needing to specify each file individually.
Next.js convention
Load environment files using the Next.js convention:
# Setup environment files
$ echo "HELLO=development local" > .env.development.local
$ echo "HELLO=local" > .env.local
$ echo "HELLO=development" > .env.development
$ echo "HELLO=env" > .env
// index.js
require('@dotenvx/dotenvx').config({ convention: 'nextjs' })
console.log(`Hello ${process.env.HELLO}`)
$ NODE_ENV=development node index.js
[[email protected]] injecting env (1) from .env.development.local, .env.local, .env.development, .env
Hello development local
This is equivalent to using --convention=nextjs
with the CLI:
$ dotenvx run --convention=nextjs -- node index.js
Flow convention
Load environment files using the dotenv-flow convention:
# Setup environment files
$ echo "HELLO=development local" > .env.development.local
$ echo "HELLO=development" > .env.development
$ echo "HELLO=local" > .env.local
$ echo "HELLO=env" > .env
// index.js
require('@dotenvx/dotenvx').config({ convention: 'flow' })
console.log(`Hello ${process.env.HELLO}`)
$ NODE_ENV=development node index.js
[[email protected]] injecting env (1) from .env.development.local, .env.development, .env.local, .env
Hello development local