Search code examples
gradlenpmintellij-ideapath

IntelliJ IDEA not picking up PATH and failing to find npm


IntelliJ IDEA 2024.1 (same problem in previous versions) on mac OS 14.4.1, the relevant portion of build.gradle looks like this:

def buildAssets = tasks.register('buildAssets') {
  ...

  doLast {
    ...
    exec {
      workingDir "$projectDir/assets"
      commandLine 'npm', 'install'
    }
  }

Upon running, I get the following error:

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

If I supply the full path to the npm command (which I have from Homebrew), all works fine:

def buildAssets = tasks.register('buildAssets') {
  ...

  doLast {
    ...
    exec {
      workingDir "$projectDir/assets"
      commandLine '/opt/homebrew/bin/npm', 'install'
    }
  }

What are some places in the user profile/IDEA config/project config that I should investigate?

Investigation so far

In IntelliJ Terminal: "It's there!"

% which npm
/opt/homebrew/bin/npm

"It's on PATH!"

% echo $PATH
/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:...

Edit:

Switching Gradle from the gradlew to a locally-installed one (/opt/homebrew/bin/gradle) has no effect.

Edit 2:

I tried every single recommendation from VonC (thanks!!), but to no avail, the error remains exactly the same. I no longer think this is about the PATH missing that homebrew folder: I found the exact spot where the exception is getting thrown, added a breakpoint, and there is the following to be seen:

screenshot of the code where the breakpoint was added

screenshot of the local variables when the breakpoint is triggered

screenshot showing the environment variables provided to the process builder, including a correct PATH

The way I am reading this debug information is that the ProcessBuilder is receiving the correct PATH, but there is something else wrong underneath. If this were a bug, would it be in IntelliJ, Gradle, Java or mac OS?

Some other things I tried

  • Checking the permissions in /opt/homebrew/bin: all seems fine.
  • Removing a folder from my PATH that contained spaces & restarting computer: does not help.

Edit 3 (VonC suggestions):

Running npm install directly in the Terminal in IDEA works just fine:

% 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 seems to be indeed a symlink:

% 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 seems to have all the permissions as expected, a standalone Java program run outside of IDEA works just fine. brew doctor reports some deprecated packages (bye bye youtube-dl I guess :( ) but no other trouble.

I will still need to check the Java/JDK version later. The fact that the debugger shows "everything is fine up to the point of executing native code within the ProcessBuilder" is now super suspicious to me.


Solution

  • After trying everything in my question above and failing, the following helped:

    % sudo launchctl config user path /opt/homebrew/bin/
    

    Inspired by here https://stackoverflow.com/a/71061556/992988