logo

Constatic

Discord Bot BaseConventions

Env

Development standards for this project

env File

With the new versions of Node.js, we can use the --env-file flag to specify an environment variables file for our project.


node --env-file .env ./dist/index.js

By starting the project with this flag in the script, the process.env object will contain all the variables defined in the .env file at the root of the project.

You can have two env files in your project and choose which one to use through predefined scripts.

package.json
{
  // ...
  "scripts":{

    "dev": "tsx --env-file .env ./src/index.ts",
    "dev:dev": "tsx --env-file .env.dev ./src/index.ts",
  }
}

If you have a .env.dev file, you can run the dev:dev script.

npm run dev:dev

This is the same for all other scripts:

npm run start:dev
npm run watch:dev

This way, you can have development and production environment variables.

Validating env variables

With zod, we can create a schema for the environment variables object to validate before the project starts, thus ensuring that the information is correct and also providing Intelisense with auto-completion of the variables we have, see:

src/settings/env.ts
import { z } from "zod";
 
const envSchema = z.object({
    BOT_TOKEN: z.string({ description: "Discord Bot Token is required" }).min(1),
    WEBHOOK_LOGS_URL: z.string().url().optional(),
    // Env vars...
});
 
type EnvSchema = z.infer<typeof envSchema>;
 
export { envSchema, type EnvSchema };

You can set the minimum number of characters, the format, whether it should be url, email, restrict values, convert to other types, see some examples below:

src/settings/env.ts
import { z } from "zod";
 
const envSchema = z.object({
    BOT_TOKEN: z.string({ description: "Discord Bot Token is required" }).min(1),
    DATABASE_URL: z.string({ description: "Database URL is required" }).url() 
    WEBHOOK_LOGS_URL: z.string().url().optional(),
    // Env vars...
});
 
type EnvSchema = z.infer<typeof envSchema>;
 
export { envSchema, type EnvSchema };

With this in mind, when working with more tools or libraries that require sensitive information stored in the .env file, simply add the necessary validations to the schema.

On this page