Linux下用Maven前端插件安装Node/NPM后npx命令未找到求助
问题:Maven Frontend Plugin安装Node/NPM后提示npx命令未找到
我在构建过程中尝试使用Maven frontend plugin安装Node和NPM,pom.xml配置如下:
<properties> <nodejs.version>v14.15.1</nodejs.version> <npm.version>6.14.8</npm.version> <npmRepoUrl> URL </npmRepoUrl> <nodeRepoUrl> URL </nodeRepoUrl> </properties> <plugin> <groupId>com.github.eirslett</groupId> <artifactId>frontend-maven-plugin</artifactId> <configuration> <nodeDownloadRoot>${nodeRepoUrl}</nodeDownloadRoot> <npmDownloadRoot>${nodeRepoUrl}</npmDownloadRoot> <nodeVersion>${nodejs.version}</nodeVersion> <npmVersion>${npm.version}</npmVersion> <workingDirectory>src/main/utilities</workingDirectory> </configuration> <executions> <execution> <id>install node and npm</id> <phase>validate</phase> <goals> <goal>install-node-and-npm</goal> </goals> </execution> <execution> <id>npm install test</id> <phase>validate</phase> <goals> <goal>npm</goal> </goals> </execution> </executions> </plugin>
日志显示Node和NPM已成功本地安装:
[INFO] Installing node version v14.15.1 [INFO] Downloading <NODE URL > [INFO] No proxies configured [INFO] No proxy was configured, downloading directly [INFO] Unpacking [INFO] Copying node binary from [INFO] Installed node locally. [INFO] Installing npm version 6.14.8 [INFO] Downloading <NPM URL > [INFO] No proxies configured [INFO] No proxy was configured, downloading directly [INFO] Unpacking [INFO] Installed npm locally.
但后续操作仍提示/bin/sh: npx: command not found错误,请问是否缺少相关配置?
解决方案
问题核心:frontend-maven-plugin会把Node/NPM安装到项目本地目录(默认是
${project.build.directory}/node),不会自动添加到系统环境变量PATH中,直接在shell里调用npx自然找不到命令。解决方法1:通过插件执行npx命令
在pom.xml的frontend-maven-plugin的executions中新增执行步骤,用npm目标调用npx:<execution> <id>execute-npx-command</id> <phase>validate</phase> <goals> <goal>npm</goal> </goals> <configuration> <!-- 示例:执行npx vite build,替换为你实际的命令 --> <arguments>exec npx vite build</arguments> </configuration> </execution>这种方式会使用插件安装的本地npm来调用npx,完全不需要依赖系统PATH。
解决方法2:直接指定本地npx的路径
如果需要在插件外部调用npx,可以直接引用本地安装的npx路径,比如:./target/node/node_modules/npm/bin/npx-cli.js your-command注意替换
your-command为实际要执行的命令,路径根据你的项目构建目录调整。额外注意事项
插件配置了<workingDirectory>src/main/utilities</workingDirectory>,确保执行npx时的工作目录和插件一致,或者在configuration中明确指定路径。
内容的提问来源于stack exchange,提问作者Suraj Ahirrao
相关产品推荐
相关产品推荐

