3

I'm trying to build an image of my node application but upon execution it shows the error.

PS C:\Users\vallabh\Desktop\visits> docker build .
[+] Building 3.8s (5/5) FINISHED

[internal] load build definition from Dorkerfile
=> transferring Dockerfile! 31B
=> [internal] load .dockerignore
=> => Transferring context: 2B 
=> [internal] load metadata for docker.io/library/node:alpine 
=> CACHED [1/2] FROM docker.io/library/node:alpine@sha256 :0677e437543016F6cb058d92792a14e5eb84348e3d5b4
> ERROR [2/2] RUN npm install:

The exact error is:

------
 > [2/2] RUN npm install:
#5 1.745 npm ERR! Tracker "idealTree" already exists
#5 1.748
#5 1.748 npm ERR! A complete log of this run can be found in:
#5 1.748 npm ERR! /root/.npm/_logs/2022-05-28T07_47_19_509Z-debug-0.log
------
executor failed running [/bin/sh -c npm install]: exit code: 1
PS C:\Users\vallabh\Desktop\visits> executor failed running [/bin/sh -c npm install]: exit code: 1D

How can I avoid this npm install error?

For illustration:

error

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Vishwa Vallabh
  • 31
  • 1
  • 1
  • 2

5 Answers5

4

i have included node version in Dokerfile. worked for me

FROM node:19.5.0-alpine
Amar
  • 41
  • 4
0

Check first if you have a similar issue as in here:

This issue is happening due to changes in NodeJS starting with version 15.

When no WORKDIR is specified, npm install is executed in the root directory of the container, which is resulting in this error.

Executing the npm install in a project directory of the container specified by WORKDIR resolves the issue.

In your case, make sure to set WORKDIR to where your node app resides in your image that you are building.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I have the same issue but I have a WORKDIR ```WORKDIR /app```, do you know what I can try ? – VersifiXion Jan 22 '23 at 16:07
  • @VersifiXion Check the pwd (`RUN pwd`) when you are executing `npm install` in your Dockerfile. – VonC Jan 22 '23 at 16:13
  • I'm stuck at docker build step, I can't build the image, how should I use pwd there please ? – VersifiXion Jan 22 '23 at 16:19
  • @VersifiXion That would merit its own question then, in which you can explain what image you are using, what Dockerfile and what you have tried. – VonC Jan 22 '23 at 16:24
  • Ok I just made a post https://stackoverflow.com/questions/75202024/cant-build-an-image-from-dockerfile-executor-failed-running-bin-sh-c-npm-in – VersifiXion Jan 22 '23 at 16:24
0
FROM node:10-alpine

RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app

WORKDIR /home/node/app

COPY package*.json ./

USER node

RUN npm install express

COPY --chown=node:node . .

EXPOSE 8085

CMD [ "node", "index.js" ]
upe
  • 1,862
  • 1
  • 19
  • 33
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 16 '23 at 21:39
0

I faced the same issue, then I discovered that it is different architecture, since my node_module directory was copied from my mac M1 machine to alpine base image, this caused a problem, I have just excluded the copy of node_modules when copying from dev directory to image. using copy command.

So simply the solution is to ignore copying node_modules directory:

  1. Create a file in the dev directory called .dockerignore this works the same way in .gitignore
  2. Add the following line to it

node_modules

This solved my issue and image was built successfully

Muhannad A.Alhariri
  • 3,702
  • 4
  • 30
  • 46
-1

I had this error and none of the above answers helped, the solution provided by ChatGPT helped me in this case, all I needed to do was-

  1. Remove my node modules folder

    sudo rm -r node_modules

  2. install node modules again

    npm i

  3. re-run the docker start command

    docker-compose up --build

Adarsh Singh
  • 107
  • 1
  • 5