Cannot run node: No such file or directory

Viewed 3034

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.

2 Answers

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.

Related