@cdktf/hcl2json

中文文档
0.21.0发布于 2025/06/05MPL-2.0TypeScriptCommonJS近 30 天下载 426.2万

Transform HCL into JSON

原始使用文档
文档目录5 节
  1. Usage
  2. Parse HCL strings
  3. Parse an entire directory
  4. Parse an expression
  5. Development

HCL2JSON

This is a WebAssembly wrapper for https://github.com/tmccombs/hcl2json

Usage

yarn add @cdktf/hcl2json

Parse HCL strings

import { parse } from '@cdktf/hcl2json'

const hcl = `
  variable "name" {
    description = "Name to be used on all the resources as identifier"
    type        = string
    default     = ""
  }
`

(async () => {
  // my-filename.tf isn't relevant for the functionality, just metadata
  const json = await parse('my-filename.tf', hcl)
  console.log(json)
})()

// =>

{
  "variable": {
    "name": [
      {
        "default": "",
        "description": "Name to be used on all the resources as identifier",
        "type": "${string}"
      }
    ]
  }
}

Parse an entire directory

import { convertFiles } from "@cdktf/hcl2json";

(async () => {
  const json = await convertFiles("/your/terraform/code");
  console.log(json);
})();

// => Unified JSON representation of all *.tf and *.tf.json files in the given directory

Parse an expression

import { getReferencesInExpression } from "@cdktf/hcl2json";

(async () => {
  const variables = await getReferencesInExpression(
    "main.tf",
    "This is a ${var.input} embedded",
  );
  console.log(variables);
})();

// => [{ value: "var.input", startPosition: 12, endPosition: 23 }]

Development

With yarn build a Typescript compile is triggered and a Go build with a WASM target is performed.

版本与发布记录

@cdktf/hcl2json 共发布 806 个版本,首次发布于 2021/03/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.21.0latest2025/06/051.4 年前
0.21.0-pre.170next2025/05/221.5 年前
0.21.0-pre.1652025/04/171.6 年前
0.21.0-pre.1662025/04/171.6 年前
0.20.122025/04/171.6 年前
0.21.0-pre.1642025/04/171.6 年前

关于 @cdktf/hcl2json 的常见问题

@cdktf/hcl2json 是什么?

Transform HCL into JSON

如何安装 @cdktf/hcl2json?

在项目目录运行 npm install @cdktf/hcl2json,或使用 pnpm add / yarn add / bun add。

@cdktf/hcl2json 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cdktf/hcl2json 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cdktf/hcl2json 可以商用吗?

@cdktf/hcl2json 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@cdktf/hcl2json 的下载量有多少?

根据 npm 官方下载统计,@cdktf/hcl2json 在 2026/10/10 – 2026/11/08 这 30 天内被下载 4,261,855 次,日均约 142,062 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cdktf/hcl2json 还在维护吗?

最新版本 0.21.0 发布于 2025/06/05(1.4 年前);至今共发布 806 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/03/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。