Search code examples
eclipsemacososx-elcapitan

Eclipse - ant task fails to run npm with "Cannot run program "npm": error=2, No such file or directory"


I'm trying to run npm in an ant task that I run from within Eclipse, but it fails.

Cannot run program "npm": error=2, No such file or directory

My terminal and system path can run npm, fine, and I've tried various solutions suggesting how to ensure an app started with Finder can use the system path, but none of them helped.

What's the way to solve this problem? I'm using Mac OSX El Capitan, and using zsh. In the past, I had symlinked npm to /usr/bin, but that's not allowed on El Capitan.


Solution

  • I've got 3 working solutions right now,

    1) So far, my best solution is to launch Eclipse using an alternate application launcher you can easily create using Apple Script.

    1. Create an Apple Script like by Running "Script Editor", clicking "New Document", and supplying the following:

        tell application "Terminal"
           do script "open -a Eclipse.app; exit"
           delay 5
            quit
        end tell
      
    2. Export that as an application, by

      • choosing "File | Export"
      • choose "File Format" as "Application"
      • Click "Save", and call it whatever you like. I called mine EclipseLauncher.app, and I saved it under Applications
    3. Run that instead of Eclipse.app.

    Note: If you have Eclipse running, you have to shut it down before trying this out.

    2) Start Eclipse from the terminal - it will pick up the system PATH defined there, and find npm when I try and run an ant task.

    $  open /Applications/Eclipse.app
    

    3) Change how Eclipse launches the ant task:

    • Right click on the ant task in the Ant view in Eclipse.
    • Choose "Run As | Ant Build ..."
    • Go to the "Environment" tab
    • Click "Select..." and then find the "PATH" variable and add it by clicking it's checkbox, then OK.
    • Edit the "Value" for your PATH variable to include the path to npm, e.g. /usr/bin/:/usr/local/bin, or if you're on Windows, probably add this to your PATH, C:\Program Files\nodejs. Windows users may be able to solve this globally by ensuring npm is in your system Windows path

    Note that you can find the path to npm in your terminal by running the following:

    $ dirname $(which npm)