【问题标题】:Cannot run program "npm" in directory无法在目录中运行程序“npm”
【发布时间】:2014-05-07 15:28:28
【问题描述】:

当我遍历具有package.JSON 和 gruntfile 的 src/main/app/ 文件夹结构时,我可以运行 npm installgrunt 命令。但是当我试图在 POM 文件存在时运行项目根文件夹中的mvn jetty:run 和属性文件时,它会抛出错误,它无法在文件夹结构src/main/app/ 中运行npm install

这是确切的错误:

[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]

这是一台 Windows 机器。

【问题讨论】:

  • 错误中提到无法运行npm,而你声称可以运行npm-install。这些不一样。
  • 是的,当我运行 npm install 时,我得到以下输出 - npm WARN package.json webapp@0.0.0 没有描述 npm WARN package.json webapp@0.0.0 没有存储库字段。 npm WARN package.json webapp@0.0.0 没有 README 数据
  • 你运行的是npm install还是npm-install
  • 当我只运行 npm 时,它会给我 npm 的使用情况
  • 您是否在完全相同的命令提示符下运行npmmvn

标签: node.js maven gruntjs


【解决方案1】:

我使用此解决方法构建了一个跨平台的 Maven:将 npm 可执行文件名称声明为 Maven 变量,并在 Windows 上运行时使用 Maven 过滤器修改此可执行文件名称。

Grunt、Bower 等也可以这样工作。

如果您使用 exec-maven-plugin >=1.6.0,则不再需要此解决方法(感谢 Manmay 在 cmets 中提供的信息):这是此插件的错误(请参阅https://github.com/mojohaus/exec-maven-plugin/issues/42),已在 1.6.0 中修复(参见 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>

【讨论】:

  • 这是一个很好的解决方案。我曾经在每个配置文件中有两个单独的插件调用(一个用于 Windows,一个用于 Unix)。这确实减少了我的 pom 文件中的行。谢谢!
  • 有史以来最好的解决方案!恭喜!
  • 如果您使用的是插件版本 1.6.0,则无需更改任何内容
  • @Manmay 你的评论应该被标记为答案。经过数小时的谷歌搜索,除了 cmets 没有什么可阅读的了。终于找到了你的 cmets,它神奇地工作了。
【解决方案2】:

在 Windows 平台中,使用 npm.cmd 替换 npm

【讨论】:

  • 这会使非 Windows 操作系统上的构建中断,如何解决这个问题以使其成为操作系统独立
  • 如果你使用maven,就选择一个插件,比如frontend-maven-plugin。见this。它将根据您的操作系统选择命令details
【解决方案3】:

显然您使用的是 Windows 系统。 npm 是一个批处理文件,而不是可执行文件。有 issues 从 maven exec 插件运行批处理文件。您可能想探索链接中建议的解决方法,例如

  • 将 .bat 脚本解构为其实际命令
  • 使用 cmd.exe 并将节点作为参数传递 - refer to this

【讨论】:

  • @Raghuram 如何在 pom.xml 中使 npm install OS 独立?我正在做一个项目,我们需要能够在 Linux 和 Windows 上进行构建。
【解决方案4】:

详情见链接:https://stackoverflow.com/a/48184182/4282901

在安装节点的目录中重命名批处理文件,以便选择现有的 npm.cmd 文件。请看下面的截图:

如果您构建同时针对 linux 和 windows 的项目,则此方法更可取。此外,如果没有。 pom 文件也很大。

【讨论】:

    【解决方案5】:

    确保将 node 和 npm 的安装目录添加到您的 PATH 变量中。如果是,您根本不必更改 .pom 文件。这是在 1.6.0 上测试的,因此如果您使用 1.5.0,您可能必须使用 @Mossroy 提到的解决方法。

    【讨论】:

      【解决方案6】:

      npm 是一个 shell 脚本。

      在 Windows 上将它重命名为 npm.sh 对我有用。

      Windows 搜索“npm”但没有找到, 然后它会尝试存在的 npm.bat

      【讨论】:

      • 路径混乱的可能性更大。我不建议重命名节点(或任何其他)已安装工具的文件。
      猜你喜欢
      • 2018-06-17
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 2020-07-11
      相关资源
      最近更新 更多