Skip to content

Env

Vona loads environment files based on multi-dimensional variables, providing a more flexible configuration mechanism and supporting more complex business scenarios

meta & .env file

Vona uses dotenv to load environment variables from the following files in the directory env:

txt
.env                # loaded in all cases
.env.[meta]         # only loaded in specified condition
.env.mine           # loaded in all cases, ignored by git
.env.[meta].mine    # only loaded in specified condition, ignored by git
  • [meta] can be any combination of the following two variables
NameDescription
mode'test' |'dev' | 'prod'
flavor'normal' |'demo' |'docker' | 'ci' | keyof VonaMetaFlavorExtend

npm scripts

Corresponding to the multidimensional variables, the correspondence between the commands and the scripts are as follows:

bash
$ npm run test
$ npm run dev
$ npm run build
$ npm run build:docker
json
"scripts": {
  "test": "vona :bin:test --flavor=normal",
  "dev": "vona :bin:dev --flavor=normal",
  "build": "vona :bin:build --flavor=normal",
  "build:docker": "vona :bin:build --flavor=docker", 
}

For example

Execute npm run dev on the command line, then the corresponding meta variable values are:

NameValue
mode'dev'
flavor'normal'

The system will automatically load the environment variables in the following files and merge them:

txt
.env
.env.normal
.env.normal.dev
.env.mine
.env.normal.mine
.env.normal.dev.mine

Tree-shaking

VonaJS only supports tree-shaking during builds for the following environment variables: META_MODE, META_FLAVOR, and NODE_ENV

For example:

typescript
if (process.env.META_MODE === 'dev') {
  console.log('for development')
}

During builds, this is automatically converted to:

typescript
if ('build' === 'dev') {
  console.log('for development')
}

Since the condition is false, tree-shaking is performed

Obtaining Environment Variables

1. process.env

For environment variables that support tree-shaking, use process.env

typescript
process.env.META_MODE
process.env.META_FLAVOR
process.env.NODE_ENV
  • process.env.NODE_ENV: For compatibility with the Node.js ecosystem only; process.env.META_MODE is preferred

2. app.meta.env

For environment variables that don't support tree-shaking, use app.meta.env to obtain them

typescript
this.app.meta.env.APP_NAME
this.app.meta.env.APP_TITLE
this.app.meta.env.SERVER_LISTEN_PORT

Built-in env variables

VonaJS provides several built-in env variables:

meta

NameDescription
META_MODEmode
META_FLAVORflavor
NODE_ENVtest/development/production

app

NameDescription
APP_NAMEApp Name
APP_TITLEApp Title
APP_VERSIONApp Version

server

NameDescription
SERVER_KEYS
SERVER_GLOBALPREFIX
SERVER_PUBLICDIR
SERVER_LOGGERDIR
SERVER_SUBDOMAINOFFSET
SERVER_WORKERS
SERVER_LISTEN_HOSTNAME
SERVER_LISTEN_PORT
SERVER_LISTEN_DISABLE

project

NameDescription
PROJECT_DISABLED_MODULESList of disabled modules
PROJECT_DISABLED_SUITESList of disabled suites

logger

NameDescription
LOGGER_CLIENT_DEFAULT
LOGGER_DUMMY
LOGGER_ROTATE_ENABLE
LOGGER_ROTATE_FILENAME
LOGGER_ROTATE_DATEPATTERN
LOGGER_ROTATE_MAXSIZE
LOGGER_ROTATE_MAXFILES

build

NameDescription
BUILD_OUTDIRSpecify the output directory
BUILD_SOURCEMAPSourcemap
BUILD_MINIFYWhether to enable minify
BUILD_INLINEDYNAMICIMPORTS
BUILD_LOG_CIRCULAR_DEPENDENCY
BUILD_COPY_DISTCopy dist to the specified directory
BUILD_COPY_RELEASECopy dist-releases to the specified directory
BUILD_DIALECT_DRIVERS

test

NameDescription
TEST_CONCURRENCY
TEST_ONLY
TEST_WHYISNODERUNNING
TEST_PATTERNS_IGNORE

database

Vona supports multiple databases. In order to use it out of the box, two datasource definitions are provided: pg/mysql, and the default datasource is pg

NameDescription
DATABASE_DEFAULT_CLIENTthe default datasource
  • pg
NameDescription
DATABASE_CLIENT_PG_HOST
DATABASE_CLIENT_PG_PORT
DATABASE_CLIENT_PG_USER
DATABASE_CLIENT_PG_PASSWORD
DATABASE_CLIENT_PG_DATABASE
  • mysql
NameDescription
DATABASE_CLIENT_MYSQL_HOST
DATABASE_CLIENT_MYSQL_PORT
DATABASE_CLIENT_MYSQL_USER
DATABASE_CLIENT_MYSQL_PASSWORD
DATABASE_CLIENT_MYSQL_DATABASE

redis

NameDescription
REDIS_DEFAULT_HOST
REDIS_DEFAULT_PORT
REDIS_DEFAULT_PASSWORD
REDIS_DEFAULT_DB

Released under the MIT License.