Applications often need to be configured to run in different environments. For example, you might want to use a different database in development than in production.
Thanks to dotenv, KoalaTs 🐨 provides a powerful way to manage your application configuration.
A fresh KoalaTs application contains a .env file in the root of your project. This file contains the default values
needed to run your application. It also comes with a .env.test file that contains the default values for your tests.
You can access configuration values using the process.env object. For example, to access the PORT value, you can use
the following code:
const port = process.env.PORT;
If you need to override an environment value (e.g. to a different value on your local machine), you can do that in a
.env.local file:
# .env.local
DATABASE_URL="mysql://root:@127.0.0.1:3306/my_database_name"
The .env.local file should not be committed to your repository. It is meant to be used for local development only.
Several other .env files are available to set environment variables in just the right situation:
.env: defines the default values of the env vars needed by the application;.env.local: overrides the default values for all environments but only on the machine which contains the file. This
file should not be committed to the repository and it’s ignored in the test environment (because tests should
produce the same results for everyone);.env.<environment> (e.g. .env.test): overrides env vars only for one environment but for all machines (these files
are committed);.env.<environment>.local (e.g. .env.test.local): defines machine-specific env var overrides only for one
environment. It’s similar to .env.local, but the overrides only apply to one environment.The environment is selected by the NODE_ENV environment variable. If you don’t set it, the default value is
development. You can set it before running your application:
NODE_ENV=production npm start
Or in your package.json scripts:
{
"scripts": {
"start": "NODE_ENV=production node dist/index.js"
}
}