简体   繁体   English

如何自动更新 package.json 中的条目?

[英]How do I update the entries in package.json automatically?

I have package.json which is below我有下面的package.json

{
  "name": "b",
  "version": "1.0.0",
  "description": "",
  "main": ".eslintrc.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Now, I want to update "name" from b to new-b .现在,我想将"name"b更新为new-b How can I do that programmatically?我怎样才能以编程方式做到这一点? I tried npm config set command, but it does not update package.json file我试过npm config set命令,但它没有更新package.json文件

Please let me know Thank you请告诉我谢谢

If you're trying to use the CLI to modify the package properties, you should be able to use the pkg command .如果您尝试使用 CLI 修改包属性,您应该能够使用pkg 命令 Though I seemed to be running into some issues with it on npm 7.5.4.虽然我似乎在 npm 7.5.4 上遇到了一些问题。

npm pkg set name new-b

Edit : Whelp turns out that this command was actually added 2 weeks prior to this post, so you'll actually need at least npm version v7.20.0 to correctly run the command.编辑:Whelp 发现这个命令实际上是这篇文章之前 2 周添加的,所以你实际上至少需要 npm 版本 v7.20.0 才能正确运行该命令。

If you want to use a JavaScript file, you can use the fs module to change the package.json files contents.如果要使用 JavaScript 文件,可以使用 fs 模块更改 package.json 文件的内容。

For example, you could have a build.js file which asks you which content to change and then uses fs to read the current file and overwrite it.例如,您可以有一个 build.js 文件,它会询问您要更改哪些内容,然后使用 fs 读取当前文件并覆盖它。 Refer to this Stack Overflow question for help using fs with JSON objects.有关将 fs 与 JSON 对象结合使用的帮助,请参阅此 Stack Overflow 问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM