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.
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.
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.
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:
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:
phoneNumber: <%= phoneNumber %>
Then, in your Node.js application, you can use EJS to pass in the phoneNumber argument like this:
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:
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.
Top comments (1)
deyml will help you!
npmjs.com/package/deyml
Loads environment structured objects from .yml for nodejs projects.