{"id":11024,"library":"hcl2-parser","title":"HCL v2 Parser for JavaScript","description":"hcl2-parser is a JavaScript library designed for parsing HashiCorp Configuration Language (HCL) version 2 strings, providing crucial utility for developers working with Terraform configurations or other systems that utilize HCLv2. It offers functions to convert HCL input into either a JSON string or a JavaScript object. A key differentiator for this package is its support for the newer HCL v2 syntax, which includes features like for-expressions, dynamic blocks, and null values, an capability often lacking in other JavaScript-based HCL parsers. The library achieves this by wrapping the Go-based `tmccombs/hcl2json` tool and transpiling the necessary Go code to JavaScript using GopherJS, resulting in a self-contained npm package. As of its initial stable release, version 1.0.3, the package includes TypeScript definitions and is primarily intended for use within Node.js environments. Its release cadence is currently nascent.","status":"active","version":"1.0.3","language":"javascript","source_language":"en","source_url":"https://github.com/benc-uk/hcl2-parser","tags":["javascript","terraform","hcl","json"],"install":[{"cmd":"npm install hcl2-parser","lang":"bash","label":"npm"},{"cmd":"yarn add hcl2-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add hcl2-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For named ESM imports of specific parser functions.","wrong":"import hcl from 'hcl2-parser'; const result = hcl.parseToString(...);","symbol":"parseToString","correct":"import { parseToString } from 'hcl2-parser';"},{"note":"For named ESM imports of specific parser functions.","symbol":"parseToObject","correct":"import { parseToObject } from 'hcl2-parser';"},{"note":"Correct ESM namespace import syntax. The 'hcl = from' syntax in some examples is incorrect.","wrong":"import * as hcl = from 'hcl2-parser';","symbol":"hcl (namespace)","correct":"import * as hcl from 'hcl2-parser';"},{"note":"Standard CommonJS `require` for Node.js environments. ESM `import` will not work directly in CJS modules without transpilation.","wrong":"import hcl from 'hcl2-parser';","symbol":"hcl (CommonJS)","correct":"const hcl = require('hcl2-parser');"}],"quickstart":{"code":"import * as hcl from 'hcl2-parser';\n\nconst hclString = `\n# Create a resource group\nvariable \"azureRegion\" {\n  type = string\n  default = \"uksouth\"\n}\nresource \"azurerm_resource_group\" \"example\" {\n  name     = \"example-resources\"\n  location = var.azureRegion\n}\n`;\n\n// Parse into a JSON string\nconst stringResult = hcl.parseToString(hclString);\nconsole.log('JSON String Result:', stringResult);\n\n// Parse into an object, accessing the actual result in array index 0\nconst objectResult = hcl.parseToObject(hclString);\nconsole.log('Parsed Object Resource:', objectResult[0].resource.azurerm_resource_group);\nconsole.log('Name:', objectResult[0].resource.azurerm_resource_group.example.name);\nconsole.log('Location:', objectResult[0].resource.azurerm_resource_group.example.location);\n","lang":"typescript","description":"Demonstrates parsing an HCL string into both a JSON string and a JavaScript object using ESM imports, highlighting how to access specific elements."},"warnings":[{"fix":"Always access the parsed object via `hcl.parseToObject(input)[0]`.","message":"The `parseToObject` function always returns an array, even when parsing a single HCL document. Developers must access the first element of this array (`result[0]`) to get the actual parsed object.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Access the parsed object at index 0, e.g., `hcl.parseToObject(hclString)[0].resource`.","cause":"Attempting to access properties directly on the array returned by `parseToObject` instead of its first element.","error":"TypeError: Cannot read properties of undefined (reading 'resource')"},{"fix":"Ensure your project uses consistent module syntax (either CommonJS or ES Modules) or configure your build tool (e.g., Webpack, Rollup, Babel, TypeScript) to transpile modules correctly for your target environment. For Node.js, use `.mjs` or `type: module` in `package.json` for ESM.","cause":"Incorrectly mixing ES module `import`/`export` syntax with CommonJS `require()` in the same file or environment without proper configuration/transpilation.","error":"SyntaxError: Unexpected token 'export' or ReferenceError: require is not defined"}],"ecosystem":"npm"}