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.

To load a convention from another directory, see config(path: directory, convention: 'nextjs').

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
⟐ injected 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

You can also set DOTENV_CONFIG_CONVENTION=nextjs.

$ DOTENV_CONFIG_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
⟐ injected env (1) from .env.development.local, .env.development, .env.local, .env
Hello development local

You can also set DOTENV_CONFIG_CONVENTION=flow.

$ NODE_ENV=development DOTENV_CONFIG_CONVENTION=flow node index.js