简体   繁体   English

在“ npm i”之后的多个位置更新package.json

[英]Update package.json in several places after “npm i”

I want to be able to detect any new package installed according to my package.json file, so when ever I do "npm i", it will automatically added to another section on my package.json file. 我希望能够检测到根据我的package.json文件安装的任何新软件包,因此,当我执行“ npm i”时,它将自动添加到我的package.json文件的另一部分。

For example, if I do "npm i axios", It will update in 2 places on my package.json file: on "dependencies" as usual, and on new section I created: "extDependencies". 例如,如果我执行“ npm i axios”,它将在package.json文件的2个位置更新:照常“依赖”,以及我创建的新部分“ extDependencies”。

Is there any way to detect any new installed packages? 有什么方法可以检测任何新安装的软件包?

Check this out: npm-scripts documentation 检查一下: npm-scripts文档

If you want to run a specific script at a specific lifecycle event for ALL packages, then you can use a hook script. 如果要在所有包的特定生命周期事件中运行特定脚本,则可以使用挂钩脚本。

Place an executable file at node_modules/.hooks/{eventname}, and it'll get run for all packages when they are going through that point in the package lifecycle for any packages installed in that root. 将可执行文件放在node_modules / .hooks / {eventname}上,当该软件包在该根目录中安装的所有软件包的生命周期中都经过该点时,它将为所有软件包运行。

Hook scripts are run exactly the same way as package.json scripts. 挂钩脚本的运行方式与package.json脚本完全相同。 That is, they are in a separate child process, with the env described above. 也就是说,它们处于上述env的单独子进程中。

You could use this to create a postinstall script (bash, python, node.js, etc) that reads the npm_package_name and npm_package_version environment variables and then use those to update the package.json. 您可以使用它来创建一个postinstall脚本(bash,python,node.js等),该npm_package_name读取npm_package_namenpm_package_version环境变量,然后使用它们来更新package.json。

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

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