HCL Parser for JavaScript
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
-
npm install js-hcl-parser -
yarn add js-hcl-parser -
pnpm add js-hcl-parser
Imports
- HCL
const HCL = require('js-hcl-parser')import HCL from 'js-hcl-parser'
- HCL.parse
import { parse } from 'js-hcl-parser'import HCL from 'js-hcl-parser'; const parsed = HCL.parse(hclString);
- HCL.stringify
import { stringify } from 'js-hcl-parser'import HCL from 'js-hcl-parser'; const hclOutput = HCL.stringify(jsonObject);
Quickstart
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
}
*/