You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Yarn Berry移除preinstall后,如何在yarn install前运行脚本?

在Yarn中实现install前执行脚本的替代方案

当然有办法,针对不同Yarn版本给你几个实用方案:

  • 方案1:自定义install脚本避免循环
    在package.json里新增两个脚本,把前置逻辑和install绑定:

    "scripts": {
      "pre-install": "你的前置脚本命令(比如node ./pre-install.js)",
      "install": "yarn run pre-install && yarn install --no-scripts"
    }
    

    这里--no-scripts是关键,能避免触发自定义install脚本导致循环调用,确保先执行你的前置逻辑,再跑原生的install流程。

  • 方案2:Yarn Berry自定义钩子
    如果你用的是Yarn v2+(Berry),可以直接通过配置文件加前置钩子。在项目根目录创建或修改.yarnrc.yml:

    hooks:
      beforeAllInstall: "你的前置脚本命令"
    

    这个钩子会在install的所有步骤开始前自动执行,完全符合你的需求,不用改scripts。

  • 方案3:用shell脚本封装执行流程
    不想折腾Yarn配置的话,直接写个简单的shell脚本(比如命名为my-install.sh):

    #!/bin/bash
    # 先跑你的前置任务
    node ./pre-install.js
    # 再执行yarn install
    yarn install
    

    给脚本加执行权限:chmod +x my-install.sh,之后每次执行./my-install.sh代替直接敲yarn install就行。

内容的提问来源于stack exchange,提问作者Slbox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 02:45:55