Mac下IntelliJ IDEA执行Gradle任务找不到npm的排查求助
Gradle执行npm命令找不到路径的排查问题
环境与问题场景
在Mac OS 14.4.1系统中使用IntelliJ IDEA 2024.1(旧版本也存在该问题),项目中build.gradle的相关配置如下:
def buildAssets = tasks.register('buildAssets') { ... doLast { ... exec { workingDir "$projectDir/assets" commandLine 'npm', 'install' } } }
执行该任务时出现错误:
Execution failed for task ':buildAssets'. > A problem occurred starting process 'command 'npm'' * Try: > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. * Exception is: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':buildAssets'. at ... at org.gradle.internal.concurrent.AbstractManagedExecutor$1.run(AbstractManagedExecutor.java:47) Caused by: org.gradle.process.internal.ExecException: A problem occurred starting process 'command 'npm'' at ... at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:79) ... 2 more Caused by: java.io.IOException: Cannot run program "npm" (in directory "/Users/.../my-project/assets"): error=2, No such file or directory at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25) ... 7 more Caused by: java.io.IOException: error=2, No such file or directory ... 8 more
若指定npm的完整路径(Homebrew安装路径/opt/homebrew/bin/npm),任务可正常执行:
def buildAssets = tasks.register('buildAssets') { ... doLast { ... exec { workingDir "$projectDir/assets" commandLine '/opt/homebrew/bin/npm', 'install' } } }
需排查用户配置/IDEA配置/项目配置中的哪些位置?
已完成的排查内容
终端验证PATH与npm路径
在IntelliJ终端中执行以下命令,结果正常:
% which npm /opt/homebrew/bin/npm
% echo $PATH /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:...
切换Gradle安装方式
将Gradle从项目自带的gradlew切换为本地Homebrew安装的/opt/homebrew/bin/gradle,问题依旧存在。
通用解决方案尝试无效
尝试了VonC的所有建议但均无效果,通过断点调试发现ProcessBuilder已获取正确的PATH,但仍报错。怀疑是IntelliJ、Gradle、Java或Mac OS的bug。额外尝试检查/opt/homebrew/bin权限、移除PATH中含空格的文件夹并重启,均无效。
IDE终端直接执行正常
在IDEA终端中直接执行npm install可正常运行:
% cd assets assets % npm install up to date, audited 476 packages in 788ms 60 packages are looking for funding run `npm fund` for details found 0 vulnerabilities assets %
npm为符号链接:
% ls -l $(which npm) lrwxr-xr-x 1 vektor admin 40 Apr 11 11:33 /opt/homebrew/bin/npm -> /opt/homebrew/Cellar/node/21.7.3/bin/npm
IDEA权限正常,外部独立Java程序可正常运行,brew doctor仅提示部分包已弃用。后续将检查Java/JDK版本,当前对ProcessBuilder执行原生代码阶段的异常存疑。
内容的提问来源于stack exchange,提问作者vektor
相关产品推荐
相关产品推荐

