Try it yourself with our free Toml Formatter tool — runs entirely in your browser, no signup needed.

How to Parse TOML in Node.js

How to Parse TOML in Node.js

Parsing TOML (Tom's Obvious, Minimal Language) files in Node.js is a crucial task, especially when working with configuration files or data storage. TOML is a lightweight, human-readable format that is easy to work with, but requires a parser to convert it into a usable JavaScript object. In this article, we will explore how to parse TOML in Node.js, covering the basics, edge cases, common mistakes, and performance tips.

Quick Example

Here is a minimal example of how to parse a TOML file in Node.js using the toml package:

import * as toml from 'toml';

const tomlString = `
title = 'Example'
[owner]
name = 'John Doe'
dob = 1979-05-27
`;

const parsedToml = toml.parse(tomlString);

console.log(parsedToml);
// Output: { title: 'Example', owner: { name: 'John Doe', dob: '1979-05-27' } }

To use this code, install the toml package by running npm install toml or yarn add toml.

Step-by-Step Breakdown

Let's walk through the code:

  1. import * as toml from 'toml';: We import the entire toml module and assign it to a variable named toml.
  2. const tomlString = '...': We define a TOML string containing some sample data.
  3. const parsedToml = toml.parse(tomlString);: We call the parse() method on the toml object, passing in the TOML string. This method returns a JavaScript object representing the parsed TOML data.
  4. console.log(parsedToml);: We log the parsed TOML object to the console.

Handling Edge Cases

Empty/Null Input

When dealing with empty or null input, the toml.parse() method will throw an error. To handle this, you can add a simple null check:

const tomlString = null;
if (tomlString) {
  const parsedToml = toml.parse(tomlString);
  console.log(parsedToml);
} else {
  console.log('Invalid input');
}

Invalid Input

If the input TOML string is invalid (e.g., contains syntax errors), the toml.parse() method will throw a SyntaxError. You can catch this error using a try-catch block:

try {
  const tomlString = ' invalid toml ';
  const parsedToml = toml.parse(tomlString);
  console.log(parsedToml);
} catch (error) {
  console.error('Invalid TOML:', error.message);
}

Large Input

When dealing with large TOML files, you may encounter performance issues. To mitigate this, you can use the toml.parseStream() method, which allows you to parse the TOML data in chunks:

const fs = require('fs');
const tomlStream = fs.createReadStream('large.toml');

tomlStream.on('data', (chunk) => {
  const parsedChunk = toml.parse(chunk.toString());
  console.log(parsedChunk);
});

Unicode/Special Characters

TOML supports Unicode characters, but you may encounter issues when working with special characters. To ensure proper handling, make sure to use the correct encoding when reading the TOML file:

const fs = require('fs');
const tomlString = fs.readFileSync('example.toml', 'utf8');
const parsedToml = toml.parse(tomlString);
console.log(parsedToml);

Common Mistakes

1. Forgetting to Install the toml Package

Make sure to install the toml package using npm install toml or yarn add toml.

Wrong code:

import * as toml from 'toml';

Corrected code:

npm install toml

2. Not Handling Errors

Always catch errors when using the toml.parse() method.

Wrong code:

const parsedToml = toml.parse(tomlString);

Corrected code:

try {
  const parsedToml = toml.parse(tomlString);
} catch (error) {
  console.error('Error parsing TOML:', error.message);
}

3. Not Checking for Null Input

Always check for null or empty input before parsing the TOML string.

Wrong code:

const parsedToml = toml.parse(tomlString);

Corrected code:

if (tomlString) {
  const parsedToml = toml.parse(tomlString);
} else {
  console.log('Invalid input');
}

Performance Tips

1. Use toml.parseStream() for Large Files

When dealing with large TOML files, use the toml.parseStream() method to parse the data in chunks.

2. Use Buffer Instead of Strings

When reading TOML files, use Buffer instead of strings to improve performance.

3. Avoid Parsing TOML in Loops

Avoid parsing TOML data in loops, as this can lead to performance issues. Instead, parse the data once and store it in a variable.

FAQ

Q: What is the difference between toml.parse() and toml.parseStream()?

A: toml.parse() parses the entire TOML string at once, while toml.parseStream() parses the data in chunks, allowing for more efficient handling of large files.

Q: How do I handle Unicode characters in TOML files?

A: Make sure to use the correct encoding when reading the TOML file, and use the utf8 encoding when reading the file using fs.readFileSync().

Q: Can I use TOML with other data formats, such as JSON?

A: Yes, you can use TOML in conjunction with other data formats, such as JSON. However, keep in mind that TOML has its own syntax and data types, so you may need to perform additional processing to convert between formats.

Q: How do I validate TOML data?

A: You can use the toml.validate() method to validate TOML data. This method returns a boolean indicating whether the data is valid or not.

Q: Can I use TOML with Node.js streams?

A: Yes, you can use TOML with Node.js streams. The toml.parseStream() method allows you to parse TOML data in chunks, making it suitable for use with streams.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp