@financial-times/package-json

中文文档
4.1.0发布于 2026/11/09MITCommonJS零依赖近 30 天下载 2232

This library allows you to load, manipulate and write the contents of a package.json file. It also provides a changelog detailing any changes that have been made.

原始使用文档
文档目录12 节
  1. loadPackageJson
  2. get
  3. hasChangesToWrite
  4. writeChanges
  5. getField
  6. setField
  7. removeField
  8. requireDependency
  9. removeDependency
  10. requireScript
  11. removeScript
  12. getChangelog

package-json

This library allows you to load, manipulate and write the contents of a package.json file. It also provides a changelog detailing any changes that have been made.

Usage

npm install @financial-times/package-json

loadPackageJson

After loading the specified package.json file into memory, the loadPackageJson method returns a collection of methods that can be used for changing the package.json document and writing those changes back to disk.

const loadPackageJson = require("@financial-times/package-json");
const packageJson = loadPackageJson({ filepath: `filepath/to/package.json` });

Methods returned:

get

Returns an object representing the current working state of the package.json document. This may be different to what exists on the file system if changes have not yet been written by calling the writeChanges method.

packageJson.get();

hasChangesToWrite

Checks if there are file changes to write.

packageJson.hasChangesToWrite(); // true or false

writeChanges

Writes to the package.json file.

packageJson.writeChanges(); // true

getField

Gets a specific field from the package.json object, by passing the field as an argument.

packageJson.getField("name"); // "@financial-times/package-json"

setField

Sets the value for a specific field in the package.json object and returns a changelog entry.

packageJson.setField("name", "newName");

Returns a changelog entry object:

{
  "event": "setField",
  "field": "name",
  "meta": {},
  "previousValue": "oldName",
  "alreadyExisted": false
}

removeField

Removes a specific field in the package.json object and returns a changelog entry.

packageJson.removeField("license");

Returns a changelog entry object:

{
  "event": "removeField",
  "field": "license",
  "meta": {},
  "previousValue": "MIT",
  "alreadyExisted": true
}

requireDependency

Requires a package to exist as a dependency in package.json.

packageJson.requireDependency({
  pkg: "prettier",
  version: "1.16.4",
  field: "devDependencies"
});

Returns a changelog entry object:

{
  "event": "requireDependency",
  "field": "devDependencies",
  "meta": {
    "pkg": "prettier",
    "version": "1.16.4"
  },
  "previousValue": "1.16.3",
  "alreadyExisted": true
}

removeDependency

Removes a package as a dependency from package.json.

packageJson.removeDependency({
  pkg: "prettier",
  version: "1.16.4",
  field: "devDependencies"
});

Returns a changelog entry object, or false if the dependency doesn't exist:

{
  "event": "removeDependency",
  "field": "devDependencies",
  "meta": {
    "pkg": "prettier"
  },
  "previousValue": "1.16.3",
  "alreadyExisted": true
}

requireScript

Requires a script to exist in the scripts field of package.json.

packageJson.requireScript({
  stage: "test",
  command: "npm run unit-test"
});

Returns a changelog entry object:

{
  "event": "requireScript",
  "field": "scripts",
  "meta": {
    "stage": "test"
  },
  "alreadyExisted": true
}

removeScript

Requires a script to exist in the scripts field of package.json.

packageJson.removeScript({
  stage: "lint"
});

Returns a changelog entry object:

{
  "event": "removeScript",
  "field": "scripts",
  "meta": {
    "stage": "lint"
  },
  "alreadyExisted": true
}

getChangelog

The changelog represents all the changes that have been made to the package.json object, regardless of whether they have yet been written to the file.

The changelog is made up of entry objects, which all have the following properties:

  • event - The type of event i.e. setField, requireDependency, removeDependency or requireScript
  • field - The field in package.json that was changed
  • alreadyExisted - Flag whether the field already existed
  • previousValue - Previous value of the field
  • meta - An object containing extra details about the change e.g. pkg, version, stage

You can access the changelog entries with the following methods:

  • getChangelog()
  • getChangelog.asMessages()
  • getChangelog.lastEntry()
  • getChangelog.lastEntryAsMessage()

Examples of working with the changelog

packageJson.requireDependency({
  pkg: "prettier",
  version: "1.16.4",
  field: "devDependencies"
});

packageJson.requireScript({
  stage: "test",
  command: "npm run unit-test"
});

const changelogObjects = packageJson.getChangelog();

/*
[
  {
    event: "requireDependency",
    field: "devDependencies",
    meta: {
      pkg: "prettier",
      version: "1.16.4"
    },
    previousValue: "1.16.3",
    alreadyExisted: true
  },
  {
    event: "requireScript",
    field: "scripts",
    meta: {
      stage: "test"
    },
    alreadyExisted: true
  }
]
*/

const changelogMessages = packageJson.getChangelog.asMessages();

/*
[
  "Required package [email protected] in devDependencies, previously 1.16.3",
  "Required script for stage 'test' (overwrote existing command)"
]
*/

const lastChangelogEntryObject = packageJson.getChangelog.lastEntry();

/*
{
  event: "requireScript",
  field: "scripts",
  meta: {
    stage: "test"
  },
  alreadyExisted: true
}
*/

const lastChangelogEntryMessage = packageJson.getChangelog.lastEntryAsMessage();

// "Required script for stage 'test' (overwrote existing command)"

版本与发布记录

@financial-times/package-json 共发布 7 个版本,首次发布于 2019/02/20;过去 12 个月发布了 1 个版本。

版本发布日期距今
4.1.0latest2026/11/095 天前
4.0.02023/06/303 年前
3.0.02020/10/026 年前
2.0.12019/10/167 年前
2.0.02019/04/267 年前
1.0.22019/02/227 年前

关于 @financial-times/package-json 的常见问题

@financial-times/package-json 是什么?

This library allows you to load, manipulate and write the contents of a package.json file. It also provides a changelog detailing any changes that have been made.

如何安装 @financial-times/package-json?

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

@financial-times/package-json 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/financial-times__package-json 是否存在。

@financial-times/package-json 支持 ESM 还是 CommonJS?

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

@financial-times/package-json 需要什么版本的 Node.js?

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

@financial-times/package-json 可以商用吗?

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

@financial-times/package-json 的下载量有多少?

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

@financial-times/package-json 还在维护吗?

最新版本 4.1.0 发布于 2026/11/09(5 天前);至今共发布 7 个版本,其中过去 12 个月发布了 1 个;首次发布于 2019/02/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。