npm install如果package.json被修改 [英] npm install if package.json was modified
问题描述
TL; DR:如果您的 package.json
已被修改,有没有办法让 npm安装
在运行任何npm脚本之前自动运行?
TL;DR: Is there a way to have npm install
run automatically before running any npm script if your package.json
has been modified?
您拉出或签出更新了 package.json
的分支.您运行 npm运行我的脚本
. my-script
依赖于一个新添加到 package.json
的软件包. my-script
失败.你想知道为什么.在确定翻转之前,请先运行 npm install
. my-script
成功运行.您不需要新的办公桌.
You pull or checkout a branch that updated package.json
. You run npm run my-script
. my-script
depends on a package that has newly been added to package.json
. my-script
fails. You wonder why. Before flipping over your desk you run npm install
just to be sure. my-script
runs successfully. You don't need a new desk.
我知道像 gradle
这样的构建/任务运行器工具可以确保在运行任务之前您的依赖项是最新的.我一直是(code)npm 不这样做的(次要)痛点.我偶然发现了两个我不喜欢的解决方案.
I know that build / task runner tools like gradle
make sure that your dependencies are up-to-date before running a task. I has always been a (minor) pain point that npm
doesn't do it. I stumbled over two solutions that I don't particluarly like.
您可以使用 make
并通过以下技巧使用其集成的依赖项跟踪来代替运行 package.json
中的npm脚本来运行命令:
Instead of relying on npm scripts in your package.json
to run commands you use make
and make use of its integrated dependency tracking with the following trick:
# Smart install: Only executes if package.json's
# modification date is later than node_module's
node_modules: package.json
npm install
@rm -f node_modules/.modified
@touch -m node_modules/.modified
install: node_modules
来源: https://mattandre.ws/2016/05/make-for-hipsters/
问题是您知道必须依靠 make
来运行脚本,并且失去了npm脚本的某些优势,例如方便地引用其他脚本和并行运行脚本( npm-run-全部
).如果其他人不知道 make
或在运行它时遇到问题,则与其他人一起工作也变得更加困难(Windows).这是Node/npm生态系统之外的一种古老工具,而且仅凭此智能安装优势就太昂贵了.
The problem is that you know have to rely on make
to run scripts and lose certain advantages of npm scripts such as conveniently referring to other scripts and running scripts in parallel (npm-run-all
). It's also harder to work with others if they don't know make
or have problems running it (Windows). It's an archaic tool outside of the node/npm ecosystem and too costly just for this smart install advantage.
另一种方法是添加 post-merge
git钩子.
Another way is to add a post-merge
git hook.
问题在于此解决方案是存储库本地的,无法轻松共享. npm install
将仅在git合并时自动运行.当您以任何其他方式更改 package.json
时,您仍然必须记住运行 npm install
.诚然,这只是实践中的次要问题.尽管如此,当您要运行脚本时,完全不必考虑运行 npm install
会很高兴.
The problem is that this solution is local to the repository and can't be easily shared. npm install
will only be run automatically on git merges. When you change package.json
in any other way you still have to remember running npm install
. Admittedly, that's a minor point in practice. Nonetheless, it would be nice to never have to think about running npm install
at all when you want to run a script.
来源: https://davidwalsh.name/git-hook-npm-install-package-json-modified
我想以类似于以下方式定义 package.json
:
I'd like to define my package.json
in a way similar to:
{
"scripts": {
"pre-run": "npm-smart-install",
"my-script": "…"
},
"dependencies": {
"npm-smart-install": "1.0.0"
}
}
npm-smart-install
是我希望存在的虚拟npm软件包. pre-run
是一个假设的npm-scripts生命周期挂钩.当我运行 npm run my-script
并且自任何脚本的上次运行以来已修改 package.json
时,请先运行 npm install
,然后再运行<代码>我的脚本.
npm-smart-install
is a hypothetical npm package that I wish existed. pre-run
is a hypothetical npm-scripts lifecycle hook. When I run npm run my-script
and package.json
has been modified since the last run of any script, run npm install
before running my-script
.
重复一遍:如果修改了 package.json
而不依赖于npm之外的工具,是否有一种方法可以在运行任何npm脚本之前自动运行 npm install
生态系统?
To repeat: Is there a way to have npm install
run automatically before running any npm script if your package.json
has been modified without relying on tools outside the npm ecosystem?
推荐答案
好的,这样我就完成了.在这里.您可以完全按照您在理想情况下指定的方式使用它.只需 npm install install-changed
并将其添加到自定义脚本中,例如您的示例中的 pre-run
.它应该确定是否需要 npm install
,并在需要时执行此操作.
Okay so I'm done with the package. Here it is. You can use it exactly the same way you specified in your ideal scenario. Just npm install install-changed
and add it to a custom script, like pre-run
in your example. It should figure out whether or not it needs to npm install
and does so if it needs to.
{
"scripts": {
"pre-run": "install-changed",
"my-script": "…"
},
您也可以通过编程方式执行此操作,但我认为您不需要此功能.
You can also do this programatically but I don't think you're going to need this.
let installChanged = require('install-changed')
let isModified = installChanged.watchPackage()
上面的函数做的事情完全相同,此外,它还返回一个布尔值,您可能会发现它有用.
The function above does exactly the same thing, additonally it also returns a boolean value which you might find useful.
这篇关于npm install如果package.json被修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!