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

如何在gradle node插件中为NpmInstallTask传递npm install命令行参数

com.github.node-gradle.node 插件传入 --ignore-scripts 参数的操作方法

方式1:全局生效(所有npm安装任务都自动携带参数)

直接在项目build.gradle的node扩展配置块中添加npmInstallArgs配置即可,原有其他node配置无需修改:

node {
    // 你原有配置保留,比如:
    // version = "16.14.0"
    // npmVersion = "8.3.1"
    // download = true

    // 新增以下配置
    npmInstallArgs = ['--ignore-scripts']
}

如果需要对所有NpmInstallTask类型的任务强制生效,也可以用任务批量配置的写法:

import com.github.gradle.node.npm.task.NpmInstallTask

tasks.withType(NpmInstallTask) {
    args += '--ignore-scripts'
}

方式2:仅给指定自定义Npm安装任务传参

如果你自定义了单独的Npm安装任务,只需要给这个任务单独加args配置即可:

import com.github.gradle.node.npm.task.NpmInstallTask

task myCustomNpmInstall(type: NpmInstallTask) {
    // 你原有任务配置保留,比如指定工作目录:
    // workingDir = file("${projectDir}/frontend")

    // 单独添加参数
    args += '--ignore-scripts'
}

验证配置生效

执行安装命令时加--info参数,查看控制台输出的npm执行命令,确认末尾携带了--ignore-scripts即可:

# Linux/macOS
./gradlew npmInstall --info

# Windows
gradlew.bat npmInstall --info

注意:如果你使用的是2.x及更旧版本的插件,类路径需要替换为com.moowork.gradle.node.npm.NpmInstallTask,其余配置逻辑一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 10:54:03