HCL Parser for JavaScript

1.0.1 · active · verified Tue Apr 21

The `js-hcl-parser` package provides a fully functional JavaScript parser for HashiCorp Configuration Language (HCL), enabling seamless conversion between HCL syntax and JSON objects. Currently stable at version 1.0.1, its primary differentiator is its genesis: the JavaScript code is generated directly from the official HashiCorp HCL Go repository using `gopherjs`. This approach aims to ensure high fidelity and compatibility with the canonical HCL specification, making it a reliable choice for projects needing to interact with HCL configurations in a JavaScript environment. The library offers both parsing HCL strings into JSON and stringifying JSON back into HCL, catering to diverse configuration management needs. Release cadence is not explicitly stated but typically follows updates or major changes in the upstream HCL Go library.

Install

Imports

Quickstart

Demonstrates how to parse an HCL string into a JavaScript object and how to stringify a JSON string back into HCL format.

import HCL from 'js-hcl-parser';

// Example HCL input string
const hclInput = `
scale {
  from = 72
  to = 24
}
`;

// Example JSON input object (or string representation)
const jsonInput = `
{
  "scale": {
    "from": 72,
    "to": 72
  }
}
`;

// Parse HCL to a JavaScript object
console.log('Parsed HCL:', HCL.parse(hclInput));
// Expected output: { scale: { from: 72, to: 24 } }

// Stringify a JSON string to HCL format
// Note: stringify expects a JSON string, not a JS object directly.
console.log('Stringified JSON:', HCL.stringify(jsonInput));
/* Expected output (formatting may vary slightly):
scale {
  from = 72
  to = 72
}
*/

view raw JSON →