@codemod-utils/json

中文文档
2.0.3发布于 2025/09/08MITTypeScriptES Module近 30 天下载 1.2万

Utilities for handling JSON

此软件包已被标记为弃用

Package has been renamed. Please install @codemod-utils/[email protected] or above.

原始使用文档
文档目录8 节
  1. What is it?
  2. API
  3. convertToMap, convertToObject
  4. readPackageJson
  5. validatePackageJson
  6. Compatibility
  7. Contributing
  8. License

This project uses GitHub Actions for continuous integration.

@codemod-utils/json

Utilities for handling JSON

What is it?

@codemod-utils/json helps you update files like package.json and tsconfig.json.

API

convertToMap, convertToObject

convertToMap() converts an object to a Map, while convertToObject() converts the Map back to an object. Use these two utilities to update JSONs.

[!NOTE] convertToObject() creates an object with keys in alphabetical order.

Example

Remove dependencies (if they exist) from package.json.

const dependencies = convertToMap(packageJson['dependencies']);

const packagesToDelete = [
  '@embroider/macros',
  'ember-auto-import',
  'ember-cli-babel',
  'ember-cli-htmlbars',
];

packagesToDelete.forEach((packageName) => {
  dependencies.delete(packageName);
});

packageJson['dependencies'] = convertToObject(dependencies);
Example

Configure tsconfig.json in an Ember app.

const compilerOptions = convertToMap(tsConfigJson['compilerOptions']);

compilerOptions.set('paths', {
  [`${appName}/tests/*`]: ['tests/*'],
  [`${appName}/*`]: ['app/*'],
  '*': ['types/*'],
});

tsConfigJson['compilerOptions'] = convertToObject(compilerOptions);

readPackageJson

Reads package.json and returns the parsed JSON.

[!NOTE] readPackageJson() checks that package.json exists and is a valid JSON.

Example

Check if the project, against which the codemod is run, has typescript as a dependency.

import { readPackageJson } from '@codemod-utils/json';

const { dependencies, devDependencies } = readPackageJson({
  projectRoot,
});

const projectDependencies = new Map([
  ...Object.entries(dependencies ?? {}),
  ...Object.entries(devDependencies ?? {}),
]);

const hasTypeScript = projectDependencies.has('typescript');

validatePackageJson

(Type-)Checks that the fields name and version exist, in the sense that their values are a non-empty string.

Example
import { readPackageJson, validatePackageJson } from '@codemod-utils/json';

const packageJson = readPackageJson({ projectRoot });

validatePackageJson(packageJson);

// Both guaranteed to be `string` (not `undefined`)
const { name, version } = packageJson;

Compatibility

  • Node.js v20 or above

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

版本与发布记录

@codemod-utils/json 共发布 34 个版本,首次发布于 2023/05/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.3latest2025/09/081.2 年前
2.0.22025/05/301.4 年前
2.0.12025/05/091.5 年前
2.0.02025/05/011.5 年前
1.2.02025/03/241.6 年前
1.1.152025/03/071.7 年前

关于 @codemod-utils/json 的常见问题

@codemod-utils/json 是什么?

Utilities for handling JSON注意:该包已被作者标记为弃用——Package has been renamed. Please install @codemod-utils/[email protected] or above.

如何安装 @codemod-utils/json?

在项目目录运行 npm install @codemod-utils/json,或使用 pnpm add / yarn add / bun add。

@codemod-utils/json 支持 TypeScript 吗?

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

@codemod-utils/json 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@codemod-utils/json 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js 20.* || >= 22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@codemod-utils/json 可以商用吗?

@codemod-utils/json 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@codemod-utils/json 的下载量有多少?

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

@codemod-utils/json 还在维护吗?

最新版本 2.0.3 发布于 2025/09/08(1.2 年前);至今共发布 34 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/05/17。作者已将其标记为弃用,新项目建议寻找替代方案。