20

When i am traversing the to src/main/app/ folder structure where i have the package.JSON & gruntfile, i am able to run npm install and grunt command. But when i am trying to run the mvn jetty:run and a property file in the root folder of the project when POM file is present, it is throwing error that it cannot run npm install in the folder structure src/main/app/.

This is the exact error:

[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (n
pminstall) on project my-abc-web: Command execution failed. Cannot
 run program "npm" (in directory "C:\Users\Achyut_J01\Documents\GitHub\infras\my-abc\my-abc-web\src\main\app"): CreatePro
cess error=2, The system cannot find the file specified -> [Help 1]

It's a Windows Machine.

Achyut
  • 768
  • 3
  • 11
  • 28
  • The error mentions not being able to run `npm`, and you claim to be able to run `npm-install`. Those aren't the same. – Biffen Mar 28 '14 at 09:19
  • yes, when i am running npm install , i am geting the following output - npm WARN package.json webapp@0.0.0 No description npm WARN package.json webapp@0.0.0 No repository field. npm WARN package.json webapp@0.0.0 No README data – Achyut Mar 28 '14 at 09:21
  • Are you running `npm install` or `npm-install`? – Biffen Mar 28 '14 at 09:23
  • When i only run npm the it gives me the npm usage – Achyut Mar 28 '14 at 09:24
  • Do you run `npm` and `mvn` in the exact same command prompt? – Biffen Mar 28 '14 at 09:24
  • yes i run both mvn and npm in same propmt & i am using npm install(without hyphen) – Achyut Mar 28 '14 at 09:25
  • And what if you give the full `npm` path, like `c:\..\npm` to your `maven-exec-plugin` ? – krampstudio Mar 28 '14 at 11:20
  • @krampstudio If you see the error message it is executing the complete path starting from c:\ – Achyut Mar 28 '14 at 12:32

6 Answers6

16

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>
Mossroy
  • 710
  • 6
  • 11
  • 1
    That's a great solution. I used to have two separated plugin calls in each profile (had one for Windows and one for Unix). This really has reduced lines in my pom file. Thank you! –  Sep 26 '16 at 14:48
  • 1
    Best solution ever! Congrats! – Alberto Anderick Jr Mar 16 '17 at 20:44
  • 3
    You dont have to change anything if you are using plugin version 1.6.0 – Manmay Mar 27 '18 at 08:39
  • 1
    @Manmay Your comment should be marked as answer. After hours of googling, there's nothing to read apart from comments. Finally found your comments and it worked magically. – Laxman Spidey Nov 20 '20 at 13:37
10

In Windows Platform, use npm.cmd to replace npm

dadanier
  • 161
  • 2
  • 8
  • 2
    This would make the build break on non Windows OSes, how could this be solved to become OS **independent**? – tirithen Jan 07 '16 at 10:48
  • if you use maven, just choose to a plugin, such as [frontend-maven-plugin](https://github.com/eirslett/frontend-maven-plugin). see [this](https://github.com/eirslett/frontend-maven-plugin#running-npm). It will choose command depends on your OS [details](https://github.com/eirslett/frontend-maven-plugin#frontend-maven-plugin) – dadanier Aug 19 '16 at 08:26
4

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.
Community
  • 1
  • 1
Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • @Raghuram How would I make npm install OS **independent** in the pom.xml? I'm working on a project where we need to be able to build on both Linux and Windows. – tirithen Jan 07 '16 at 10:45
0

See the link for details: https://stackoverflow.com/a/48184182/4282901

In the directory where node is installed rename the batch file so that the existing npm.cmd file is picked. See screenshot below: rename npm so that npm.cmd is picked by default in windows

This method is preferable if you build the projects targeting linux and windows both. Moreover, also if the no. of pom files is also large.

Syed Osama Maruf
  • 1,895
  • 2
  • 20
  • 37
0

Make sure that the directory in which node and npm are installed is added to your PATH variable. If it is, you shouldn't have to change your .pom files at all. This is tested on 1.6.0, so you may have to use the workaround mentioned by @Mossroy if you use 1.5.0.

Trenton Telge
  • 478
  • 3
  • 17
-2

npm is a shell script.

renaming it npm.sh on windows worked for me.

Windows searched 'npm' and did not find, it then tries npm.bat which exists

  • I'ts more likely that the paths are messed up. I wouldn't recommend renaming files of a node (or any other) installed tool. – sneusse Oct 28 '20 at 22:25