Cannot run program "npm" in directory

I used this workaround to have a cross-platform Maven build : declare the npm executable name as a Maven variable, and use Maven filters to modify this executable name when running on Windows.

It can work the same for Grunt, Bower etc.

This workaround is not necessary any more if you use exec-maven-plugin >=1.6.0 (thanks Manmay for the information in the comments): it was a bug of this plugin (see https://github.com/mojohaus/exec-maven-plugin/issues/42), that has been fixed in 1.6.0 (see https://github.com/mojohaus/exec-maven-plugin/pull/46)

<properties>
    <npm.executable>npm</npm.executable>
</properties>

(...)

<build>
    <plugins>
        (...)
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.5.0</version>
            <executions>
                <execution>
                    <id>exec-npm</id>
                    <phase>process-resources</phase>
                    <configuration>
                        <executable>${npm.executable}</executable>
                        <arguments>
                            <argument>install</argument>
                        </arguments>
                    </configuration>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        (...)
    </plugins>
</build>
<profiles>
    <profile>
        <id>platform-windows</id>
        <activation>
            <os>
                <family>windows</family>
            </os>
        </activation>
        <properties>
            <!-- Override the executable names for Windows -->
            <npm.executable>npm.cmd</npm.executable>
            <grunt.executable>grunt.cmd</grunt.executable>
            <bower.executable>bower.cmd</bower.executable>
        </properties>
    </profile>
</profiles>

Evidently you are on a Windows system. npm is a batch file and not an executable. There are issues running a batch file from maven exec plugin. You may want to explore the workaround suggested in the link, like

  • deconstruct the .bat script into its actual commands
  • use cmd.exe and pass node as parameter - refer to this.

In Windows Platform, use npm.cmd to replace npm