Toml files are used for configuration settings.
This tutorial explains about following things.
- Create a Node Project and add the required dependencies
- Read toml file in javascript node code
- Create and write toml file in NodeJS
Create a Nodejs Project
First, Create an empty directory in the terminal.
mkdir nodejs-toml-examples
Next, Change to the directory
cd nodejs-toml-examples
Next, Create a Node Project using the npm init
command. This command creates a package.json with basic project settings.
E:\work\nodejs-toml-examples>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (nodejs-toml-examples)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to E:\work\nodejs-toml-examples\package.json:
{
"name": "nodejs-toml-examples",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes)
Next, Create a src folder, that stores javascript files.
How to read toml file in Nodejs
Let’s create a config.toml file and copy the below content into it.
config.toml:
[mysql]
# mysql host
host = "localhost:31016"
# mysql username
username = "root"
# mysql user password
password = "password"
# mysql database name
database = "students"
Node provides toml
library to read and write files.
Install toml library in a project.
E:\work\nodejs-toml-examples> npm install toml
added 1 package, and audited 2 packages in 2s
Next, Create a read-toml.js
file.
Here are steps to read toml file
- Use
fs
andtoml
modules in a code. fs module is a node native module to read and write files fs
providesreadFileSync
function, takes a file name and encoding, It reads the file content into a variable.- Now, the variable holds the content of the file in the string format.
- Convert string into a javascript object using
toml.parse()
function - You can access the data in the object using dot (.) notation.
Example to read the time file. read-toml.js:
const fs = require('fs');
const toml = require('toml');
try{
// Read the TOML file into a string
const data = fs.readFileSync('config.toml', 'utf8');
// Parse the TOML data into a javascript object
const result = toml.parse(data);
console.log(result.mysql.host);
console.log(result.mysql.username);
console.log(result.mysql.password);
console.log(result.mysql.database);
} catch (e) {
console.error("Parsing toml content on line " + e.line + ", column " + e.column +
": " + e.message);
}
Running the above code in the terminal outputs
E:\work\nodejs-toml-examples> node read-toml
localhost:31016
root
password
students
How to write to toml file from an object in Node javascript
Following are steps to write to toml file
- use fs and toml modules in a code
- Create a JSOn object that holds key and value pairs
- pass an object to toml.stringify() method, Convert the object toml format
- Write to file using fs.writeFileSync() method takes a file name
- toml file created on running the code
const fs = require('fs');
const toml = require('toml');
const obj = {
mysql: {
host: "localhost",
username: "root",
password: "password"
}
};
// Convert the object to TOML format
const str = toml.stringify(obj);
// Create the TOML string to a file
fs.writeFileSync('result.toml', str, 'utf8');
You can check source code for running code example.