node.js 无法在目录中运行程序“npm”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22708255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot run program "npm" in directory
提问by Achyut
When i am traversing the to src/main/app/folder structure where i have the package.JSON& gruntfile, i am able to run npm installand gruntcommand. But when i am trying to run the mvn jetty:runand a property file in the root folder of the project when POM file is present, it is throwing error that it cannot run npm installin the folder structure src/main/app/.
当我遍历src/main/app/我有package.JSON& gruntfile 的文件夹结构时,我能够运行npm install和grunt命令。但是,当mvn jetty:run存在 POM 文件时,当我尝试在项目的根文件夹中运行和 属性文件时,会抛出无法npm install在文件夹结构中运行的错误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.
这是一台Windows机器。
采纳答案by Raghuram
Evidently you are on a Windows system. npm is a batch file and not an executable. There are issuesrunning a batch file from maven exec plugin. You may want to explore the workaround suggested in the link, like
显然您使用的是 Windows 系统。npm 是批处理文件,而不是可执行文件。从 maven exec 插件运行批处理文件时出现问题。您可能想探索链接中建议的解决方法,例如
- deconstruct the .bat script into its actual commands
- use cmd.exe and pass node as parameter - refer to this.
- 将 .bat 脚本解构为它的实际命令
- 使用 cmd.exe 并将节点作为参数传递 -请参阅此.
回答by Mossroy
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.
我使用此解决方法进行跨平台 Maven 构建:将 npm 可执行文件名称声明为 Maven 变量,并在 Windows 上运行时使用 Maven 过滤器修改此可执行文件名称。
It can work the same for Grunt, Bower etc.
它可以为 Grunt、Bower 等工作。
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)
如果您使用 exec-maven-plugin >=1.6.0(感谢 Manmay 在评论中提供信息),则不再需要此解决方法:这是此插件的错误(请参阅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>
回答by dadanier
In Windows Platform, use npm.cmdto replace npm
在 Windows 平台下,使用npm.cmd替换npm
回答by Syed Osama Maruf
See the link for details: https://stackoverflow.com/a/48184182/4282901
详情见链接: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:

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

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.
如果您构建针对 linux 和 windows 的项目,则此方法更可取。此外,如果没有。pom 文件也很大。

