In this tutorial, How to read and write a properties file content in Nodejs Application.

Usually, Properties files are used to store environment information as well as settings related to users or the environment.

Nodejs has a properties-reader npm library to read and write the property files.

  • Create a folder nodejs-reader
  • Inside a folder, Run the npm init command to create a nodejs scaffolding application
npm init -Y

This will create a basic nodejs folder Next, Install properties-reader npm library

npm install properties-reader

Let’s see a read-and-write properties file in the nodejs application

Read properties file in Nodejs Application

In this example, you will learn how to Read the key and its values from a properties file and display it to the console

Let’s declare the properties file

database=mysql
hostname=localhost
username=john
password=

Here is an example and step by step

  • Create a new javascript file - propertiesreader.js
  • import properties-reader module into javascript using the required method.
  • Create an object for PropertiesReader by giving the properties file location
  • This object has all properties keys and values
  • You can get value using the get method with the given key and print to the console.

propertiesreader.js:

var PropertiesReader = require('properties-reader');
var properties = PropertiesReader('config/database.properties');

console.log(properties.get("database"));
console.log(properties.get("hostname"));
console.log(properties.get("username"));
console.log(properties.get("password"));

Output:

mysql
localhost
john

  • How to iterate or loop all keys and values in a properties file?

It has each method to iterate all objects in a file

properties.each((key, value) => {
    console.log(key + ":" + value);
});

Output:

database:mysql
hostname:localhost
username:john
password:

How to write keys and values to a properties file in javascript

These example examples read properties in Java with line-by-line

  • created a File object with an absolute path
  • Create BufferedReader using FileReader object
  • get the First line of the properties file using readLine of BufferedReader
  • Loop using a while loop until the end of the line reached
  • Print each line
var PropertiesReader = require('properties-reader');
const propertiesPah = "config/database.properties";
const properties = PropertiesReader(propertiesPah, { writer: { saveSections: true } });

properties.set("key1", "value1")
properties.set("database", "postgres")
properties.save(propertiesPah, function then(err, data) {
    if (err) {
        console.log("error in write a properties file")

    }
    console.log("saved data to properties file")

});






How to write a key and values to a properties file in Javascript

In this example, You can read and write a property using

  • Import PropertiesReader into javascript
  • Create a writer object using PropertiesReader with path and writer object
    • You can add new properties or update existing properties if the properties file exists
  • set method do update or add key and values
  • The save method of the properties object writes to the properties file.

Here is a complete example of reading and writing a properties file

var PropertiesReader = require('properties-reader');
const propertiesPath = "config/database.properties";
const writer = PropertiesReader(propertiesPath, { writer: { saveSections: true } });

console.log(writer.get("database"));
console.log(writer.get("hostname"));
console.log(writer.get("username"));
console.log(writer.get("password"));

writer.each((key, value) => {
    console.log(key + ":" + value);
});

writer.set("key1", "value1")
writer.set("database", "value1")

writer.save(propertiesPath, function then(err, data) {
    if (err) {
        console.log("error in write a properties file")

    }
    console.log("saved data to properties file")

});

The output of a file

database=postgres
hostname=localhost
username=john
password=
key1=value1

Conclusion

You learned to read a properties file with keys and values as well and also write keys and values to a properties file in the NodeJS javascript application