Gradle failed to execute npm command

I try to run the npm command inside the gradle task, but I get a strange error:

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" } 

Can anyone help?

+8
source share
4 answers

This answer worked for me with another npm related task. The recommendation is to use the executable and args, not the command line.

executable 'npm' args ['install']

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

As an alternative to the Gradle Node plugin, it is also very convenient for managing the most common Node tasks in Gradle build. I use this plugin as the basis for my Node tasks, and then create other custom tasks if necessary.

+4
source

If you are using Windows, try this:

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

instead of this:

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

If you are using Windows, you should use "npm.cmd" instead of "npm". It is better to determine if Windows is or not, and create an npm command. Please see the code snippet below,

 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' } 
+1
source

I used @ALDRIN P VINCENT answer to solve this problem. But if you need to pass command line arguments to the npm script, you can do this:

Suppose the following system properties are passed to the gradle script

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

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

 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 begins with commandLine npm ... This line corresponds to:

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

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

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

And then you can select these command line arguments through process.argv[2] and process.argv[3] .

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

0
source

Source: https://habr.com/ru/post/1243524/


All Articles