# hcl2-parser

> HCL v2 parser for JavaScript

Latest version **1.0.3** (published 2021-05-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install hcl2-parser
pnpm add hcl2-parser
yarn add hcl2-parser
bun add hcl2-parser
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2021-05-07 |
| First published | 2021-05-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.9 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Ben Coleman |
| Maintainers | bencuk |
| Keywords | terraform, hcl, json |

## Links

- npm: https://www.npmjs.com/package/hcl2-parser
- Repository: https://github.com/benc-uk/hcl2-parser
- Homepage: https://github.com/benc-uk/hcl2-parser#readme
- Issues: https://github.com/benc-uk/hcl2-parser/issues
- npm.io page: https://npm.io/package/hcl2-parser

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.3 (latest) — 2021-05-07
- 1.0.2 — 2021-05-07
- 1.0.1 — 2021-05-07
- 1.0.0 — 2021-05-07

## README

# HCL v2 parser for JS

This is a [HCL version 2](https://pkg.go.dev/github.com/hashicorp/hcl/v2#readme-changes-in-2-0) parser for JavaScript, there are several other JS parsers for HCL but none that support the newer HCL v2 syntax

It wraps the very helpful [tmccombs/hcl2json](https://github.com/tmccombs/hcl2json) and calls the convert package in order to parse HCL input strings to JSON strings. The Go code in `parser.go` is converted to JS using [GopherJS](https://github.com/gopherjs/gopherjs)

TypeScript definitions are included

## Reference

The module exports the following functions:

```ts
function parseToString(input: string): string
function parseToObject(input: string): any
```

## Usage

Install as normal with NPM

```bash
npm install hcl2-parser
```

Importing into your project

```ts
// Good old fashioned Node.js CommonJS require
const hcl = require("hcl2-parser")

// Import with ES6 or TypeScript
import * as hcl = from "hcl2-parser"
```

Simple example of usage

```js
const hcl = require("hcl2-parser")

const hclString = `
# Create a resource group
variable "azureRegion" {
  type = string
  default = "uksouth"
}
resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = var.azureRegion
}
`

// Parse into a JSON string
stringResult = hcl.parseToString(hclString)
console.log(stringResult)

// Parse into an object, the actual result is in array index 0 for reasons I don't understand
objectResult = hcl.parseToObject(hclString)
console.log(objectResult[0].resource.azurerm_resource_group)
```

---
_Source: https://npm.io/package/hcl2-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
