java Gradle 无法执行 npm 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35540126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:14:39  来源:igfitidea点击:

Gradle couldn't execute npm command

javaintellij-ideagradlenpmnpm-install

提问by Pedro Henrique

I'm trying to run a npm command inside of gradle task but I'm getting a strange error:

我正在尝试在 gradle 任务中运行 npm 命令,但出现一个奇怪的错误:

Caused by: net.rubygrapefruit.platform.NativeException: Could not start 'npm'
    at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:27)
    at net.rubygrapefruit.platform.internal.WrapperProcessLauncher.start(WrapperProcessLauncher.java:36)
    at org.gradle.process.internal.ExecHandleRunner.run(ExecHandleRunner.java:65)
    ... 2 more
Caused by: java.io.IOException: Cannot run program "npm" (in directory "/Users/psilva/Documents/projects/registrolivre"): error=2, No such file or directory
    at net.rubygrapefruit.platform.internal.DefaultProcessLauncher.start(DefaultProcessLauncher.java:25)
    ... 4 more
Caused by: java.io.IOException: error=2, No such file or directory

And this is my task:

这是我的任务:

task npmInstall(type: Exec) {
    commandLine "npm", "install"
}

Could someone help?

有人可以帮忙吗?

回答by Taylor714

This answerworked for me with a different npm-related task. The recommendation there is to use an executable and args rather than commandLine.

这个答案对我来说适用于不同的 npm 相关任务。建议使用可执行文件和 args 而不是命令行。

executable 'npm' args ['install']

executable 'npm' args ['install']

Depending on your directory structure, you may also need to add the workingDir property and set it to the directory where your package.json lives.

根据您的目录结构,您可能还需要添加 workingDir 属性并将其设置为 package.json 所在的目录。

As an alternative, the Gradle Node Pluginis also really handy for managing the most common Node tasks in a Gradle build. I use this plugin as the basis for my Node tasks and then create other custom tasks as needed.

作为替代方案,Gradle 节点插件对于管理 Gradle 构建中最常见的节点任务也非常方便。我使用这个插件作为我的 Node 任务的基础,然后根据需要创建其他自定义任务。

回答by Vikash Madhow

If you are on Windows try this:

如果你在 Windows 上试试这个:

task npmInstall(type: Exec) {
    commandLine "npm.cmd", "install"
}

instead of this:

而不是这个:

task npmInstall(type: Exec) {
    commandLine "npm", "install"
}

回答by ALDRIN P VINCENT

If you are using Windows OS, you have to use 'npm.cmd' instead of 'npm'. Better to detect whether OS is windows or not and build your npm command. Please see the code snippet below,

如果您使用的是 Windows 操作系统,则必须使用“npm.cmd”而不是“npm”。更好地检测操作系统是否为 windows 并构建您的 npm 命令。请看下面的代码片段,

import org.apache.tools.ant.taskdefs.condition.Os

task npmInstall(type: Exec) {
    String npm = 'npm';
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        npm = 'npm.cmd'
    }
    workingDir 'src/main/webapp'
    commandLine npm, 'install'
}

回答by Faraz

I used @ALDRIN P VINCENTanswer to solve this issue. But if you need to pass command line arguments to npm script, you can do this:

我使用@ALDRIN P VINCENT 的答案来解决这个问题。但是如果你需要将命令行参数传递给 npm 脚本,你可以这样做:

Let's say following system properties are passed to gradle script

假设以下系统属性被传递给 gradle 脚本

gradle test-Dsome1=dev -Dsome2=https://www.google.com

In your test script in build.gradle, you will do this:

在 build.gradle 中的测试脚本中,您将执行以下操作:

task apifunctionaltest(type: Exec) {
String npm = 'npm';
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
    npm = 'npm.cmd'
}
commandLine npm, 'run', 'test', '--', '--some1='+System.getProperty("some1"), '--some2='+System.getProperty("some2")

}

}

The main command starts with commandLine npm… This line equates to:

主命令以 commandLine npm 开头……这一行相当于:

npm run test -- --some1=dev --some2=https://www.google.com

The test script in package.json also should have ‘npm install' (it depends) command so node modules are installed before tests run. And if modules are already installed, node will not waste time and reinstall them. test script should be something like this:

package.json 中的测试脚本也应该有“npm install”(它取决于)命令,以便在测试运行之前安装节点模块。如果模块已经安装,节点不会浪费时间重新安装它们。测试脚本应该是这样的:

"scripts": {
    "test": "npm install && webpack"
  }

And then you can pick those command line args thru process.argv[2]and process.argv[3].

然后你可以通过process.argv[2]和选择那些命令行参数process.argv[3]

If you have a simple script like mine, then some1 and some2 will be in the 2nd and 3rd position of an array, respectively.

如果您有一个像我这样的简单脚本,那么 some1 和 some2 将分别位于数组的第二个和第三个位置。