2

I execute mvn clean install inside a docker container from image maven:3-alpine to build the application. In the pom.xml I make use of the frontend-maven-plugin because I need to install node and npm and then run npm install to build the frontend (angular).

 <plugin>
        <groupId>com.github.eirslett</groupId>
        <artifactId>frontend-maven-plugin</artifactId>
        <version>1.9.1</version>
        <configuration>
          <nodeVersion>v12.16.1</nodeVersion>
        </configuration>
        <executions>
          <execution>
            <id>install node and npm</id>
            <goals>
              <goal>install-node-and-npm</goal>
            </goals>
            <phase>generate-resources</phase>
          </execution>
          <execution>
            <id>npm install</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
              <arguments>install</arguments>
              <installDirectory>./</installDirectory>
            </configuration>
          </execution>
          <execution>
            <id>ng build</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <phase>compile</phase>
            <configuration>
              <arguments>run-script build</arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>

Node and npm will be installed correctly but when it runs npm install it returns an error: Failed to run task: 'npm install' failed. java.io.IOException: Cannot run program "/var/lib/jenkins/workspace/myProject/node/node" (in directory "/var/lib/jenkins/workspace/myProject"): error=2, No such file or directory -> [Help 1]

If I enter into the container (docker exec) and try to run manually npm install it gives me again the same error. When I check if node is correctly installed then I see the file /var/lib/jenkins/workspace/myProject/node/node is there but when I try to run node from inside the /node directory myself, let's say node -v it says me again No such file or directory. I don't understand why it is happening, because node is there!! The current user has also the right to execute this file.

I did some search about the problem, some people say that installing node on ubuntu in this way is not the correct way but that happens only in my container. If I try the same on my local machine (it is also an ubuntu) then node works.

Elio
  • 428
  • 3
  • 20
  • Does it have the execute bit set in the file mode? – Henry Jun 04 '20 at 11:31
  • It looks like that: -rwxr-xr-x 1 root root 46228688 Jun 4 10:40 node drwxr-xr-x 3 108 112 4096 Jun 3 13:37 node_modules -rwxr--r-- 1 108 112 893 Jun 3 13:37 npm -rwxr--r-- 1 108 112 464 Jun 3 13:37 npm.cmd And I try to run it as root. – Elio Jun 04 '20 at 11:32

2 Answers2

0

When you run node -v on it's own it's trying to find node on your PATH. Installing via maven in this manner will not do so for you

Check out one of these answers, for how to do that on Linux.

If your user does not have permissions to do so, which I suspect may be the case given you are running in jenkins, you can instead run the commands required using the full or relative path to the node and npm binaries.

e.g. if you have a file test.js in directory /var/lib/jenkins/workspace/myProject, in that directory could run either:

./node/node test.js

or

/var/lib/jenkins/workspace/myProject/node/node

Aside: a better solution to using Maven to install NPM & Node in your CI environment would be to use nvm, Docker images, or for Jenkins use the Node.js plugin.

Peter Reid
  • 5,139
  • 2
  • 37
  • 33
  • As I said, I also tried to start the same container on my host and tried to enter it with `docker exec` and tried to execute node with root user. It doesn't work either with absolute nor relative path. I also know that there are also other ways to do it, but I still need to understand what the problem is here. The whole thing is working on my host with ubuntu, why it is not working with an alpine docker image?! – Elio Jun 05 '20 at 06:29
  • Ah sorry that wasn't clear on your question, you just said "but when I try to run manually let's say node -v" - thought you were trying to execute that command on it's own – Peter Reid Jun 05 '20 at 08:24
  • Yes, sorry. I edited my question to be more clear. thnx. – Elio Jun 05 '20 at 09:08
0

I have encountered the same issue. Apparently alpine is not working well with nodejs binary. There seem to be some workarounds thou https://github.com/eirslett/frontend-maven-plugin/issues/633.

Jiri K
  • 1