How to Pass Environment Variables to a YAML File in a Node.js Application using EJS Templating Engine
Tags: yaml, node, ejs, env
Published: 2023-04-16 11:54:07

When building Node.js applications, it's common to require passing environment arguments to an application, such as database credentials or API keys. This tutorial explores how to pass environment variables to a YAML file in a Node.js application using the EJS templating engine and how to read them in the application.

**Step 1: Install Dependencies**
First, we need to install two packages - **ejs** and **dotenv**. EJS is a templating engine, and dotenv loads environment variables from a .env file into process.env.
```sh
npm install ejs dotenv
```
**Step 2: Create a Configuration File**
Create a file named config.yaml at the root of your project, which will hold the configuration values for your application.

```yaml
port: <%= process.env.PORT %>
database:
  host: <%= process.env.DB_HOST %>
  username: <%= process.env.DB_USER %>
  password: <%= process.env.DB_PASS %>
```
In this file, we're using EJS templating syntax to insert environment variables into the configuration values.

**Step 3: Create a .env File**
Create a file named .env at the root of your project, which will hold the actual environment variables.

```sh
PORT=3000
DB_HOST=localhost
DB_USER=root
DB_PASS=password
```
**Step 4: Read Configuration File**
Now we can read the configuration file using EJS and the environment variables we just loaded. In your application's entry point file, add the following code:

```javascript
const fs = require('fs');
const ejs = require('ejs');
const yaml = require('js-yaml');
require('dotenv').config();

const configTemplate = fs.readFileSync('config.yaml', 'utf8');
const configString = ejs.render(configTemplate);

try {
  const config = yaml.load(configString, 'utf-8');
  console.log(config);
} catch (e) {
  console.log(e);
}
```
Here, we're reading the config.yaml file into a string, rendering it using EJS to insert the environment variables, and then parsing the resulting string using the js-yaml library. The resulting configuration object is then logged to the console.

Similarly, normal arguments can also be passed to YAML file using EJS. For instance, if you want to insert a user's phone number dynamically to a YAML config file only at runtime, you can follow the same steps:

```yaml
phoneNumber: <%= phoneNumber %>
```
Then, in your Node.js application, you can use EJS to pass in the _phoneNumber_ argument like this:

```javascript
const ejs = require('ejs');
const fs = require('fs');

const template = fs.readFileSync('path/to/your/config.yaml', 'utf8');
const rendered = ejs.render(template, { phoneNumber: '919999999999' });

console.log(rendered);
```
This will output the YAML code with the phoneNumber argument inserted:

```makefile
phoneNumber: 919999999999
```

**Conclusion**
In this tutorial, we have explored the process of passing environment variables and other variables as arguments to a Yaml file within a Node.js application, leveraging the power of the EJS templating engine to read and manage them seamlessly. By using a templating engine such as EJS, we can keep our configuration files concise and easy to read, while still allowing for the injection of essential environment variables when needed.