'npm ERR! Tracker "idealTree" already exists while creating the Docker image for Node project

I have created one node.js project called simpleWeb. The project contains package.json and index.js.

index.js

    const express = require('express');
    
    const app = express();
    
    app.get('/', (req, res) => {
      res.send('How are you doing');
    });
    
    app.listen(8080, () => {
      console.log('Listening on port 8080');
    });

package.json


    {
        "dependencies": {
          "express": "*"
        },
        "scripts": {
          "start": "node index.js"
        }
      }

I have also created one Dockerfile to create the docker image for my node.js project.

Dockerfile

# Specify a base image
FROM node:alpine

# Install some dependencies 
COPY ./ ./
RUN npm install

# Default command
CMD ["npm", "start"]

While I am tried to build the docker image using "docker build ." command it is throwing below error.

Error Logs

simpleweb » docker build .                                                    ~/Desktop/jaypal/Docker and Kubernatise/simpleweb
[+] Building 16.9s (8/8) FINISHED
 => [internal] load build definition from Dockerfile                                                                         0.0s
 => => transferring dockerfile: 37B                                                                                          0.0s
 => [internal] load .dockerignore                                                                                            0.0s
 => => transferring context: 2B                                                                                              0.0s
 => [internal] load metadata for docker.io/library/node:alpine                                                               8.7s
 => [auth] library/node:pull token for registry-1.docker.io                                                                  0.0s
 => [internal] load build context                                                                                            0.0s
 => => transferring context: 418B                                                                                            0.0s
 => [1/3] FROM docker.io/library/node:alpine@sha256:5b91260f78485bfd4a1614f1afa9afd59920e4c35047ed1c2b8cde4f239dd79b         0.0s
 => CACHED [2/3] COPY ./ ./                                                                                                  0.0s
 => ERROR [3/3] RUN npm install                                                                                              8.0s
------
 > [3/3] RUN npm install:
#8 7.958 npm ERR! Tracker "idealTree" already exists
#8 7.969
#8 7.970 npm ERR! A complete log of this run can be found in:
#8 7.970 npm ERR!     **/root/.npm/_logs/2020-12-24T16_48_44_443Z-debug.log**
------
executor failed running [/bin/sh -c npm install]: exit code: 1

The log file above it is providing one path "/root/.npm/_logs/2020-12-24T16_48_44_443Z-debug.log" where I can find the full logs.

But, The above file is not present on my local machine.

I don't understand what is the issue.



Solution 1:[1]

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.

Use the following Dockerfile:

# Specify a base image
FROM node:alpine

#Install some dependencies

WORKDIR /usr/app
COPY ./ /usr/app
RUN npm install

# Set up a default command
CMD [ "npm","start" ]

Solution 2:[2]

The correct answer is basically right, but when I tried it still didn't work. Here's why:

WORKDIR specifies the context to the COPY that follows it. Having already specified the context in ./usr/app it is wrong to ask to copy from ./ (the directory you are working in) to ./usr/app as this produces the following structure in the container: ./usr/app/usr/app.

As a result CMD ["npm", "start"], which is followed where specified by WORKDIR (./usr/app) does not find the package.json.

I suggest using this Dockerfile:

FROM node:alpine

WORKDIR /usr/app

COPY ./ ./

RUN npm install

CMD ["npm", "start"]

Solution 3:[3]

Global install

In the event you're wanting to install a package globally outside of working directory with a package.json, you should use the -g flag.

npm install -g <pkg>

This error may trigger if the CI software you're using like semantic-release is built in node and you attempt to install it outside of a working directory.

Solution 4:[4]

You should specify the WORKDIR prior to COPY instruction in order to ensure the execution of npm install inside the directory where all your application files are there. Here is how you can complete this:

WORKDIR /usr/app

# Install some dependencies
COPY ./ ./
RUN npm install

Note that you can simply "COPY ./ (current local directory) ./ (container directory which is now /usr/app thanks to the WORKDIR instruction)" instead of "COPY ./ /usr/app"

Now the good reason to use WORKDIR instruction is that you avoid mixing your application files and directories with the root file system of the container (to avoid overriding file system directories in case you have similar directories labels on your application directories)

One more thing. It is a good practice to segment a bit your configuration so that when you make a change for example in your index.js (so then you need to rebuild your image), you will not need to run "npm install" while the package.json has not been modified.

Your application is very basic, but think of a big applications where "npm install" should take several minutes.

In order to make use of caching process of Docker, you can segment your configuration as follows:

WORKDIR /usr/app

# Install some dependencies
COPY ./package.json ./
RUN npm install
COPY ./ ./ 

This instructs Docker to cache the first COPY and RUN commands when package.json is not touched. So when you change for instance the index.js, and you rebuild your image, Docker will use cache of the previous instructions (first COPY and RUN) and start executing the second COPY. This makes your rebuild much quicker.

Example for image rebuild:

 => CACHED [2/5] WORKDIR /usr/app                                                                                                       0.0s
 => CACHED [3/5] COPY ./package.json ./                                                                                                 0.0s
 => CACHED [4/5] RUN npm install                                                                                                        0.0s
 => [5/5] COPY ./ ./

Solution 5:[5]

specifying working directory as below inside Dockerfile will work:

WORKDIR '/app'

make sure to use --build in your docker-compose command to build from Dockerfile again:

docker-compose up --build

Solution 6:[6]

# Specify a base image
FROM node:alpine

WORKDIR /usr/app

# Install some dependencies
COPY ./package.json ./
RUN npm install
COPY ./ ./

# Default command
CMD ["npm","start"]

1.This is also write if you change any of your index file and docker build and docker run it automatically change your new changes to your browser output

Solution 7:[7]

Building on the answer of Col, you could also do the following in your viewmodel:

public class IndexVM() {

   @AfterCompose
   public void doAfterCompose(@ContextParam(ContextType.COMPONENT) Component c) {
       Window wizard = (Window) c;
       Label label = (Label) c.getFellow("lblName");
       ....
   }
}

In doing so, you actually have access to the label object and can perform all sorts of tasks with it (label.setValue(), label.getValue(), etc.).

Solution 8:[8]

A bit late to the party, but for projects not wanting to create a Dockerfile for the installer, it is also possible to run the installer from an Ephemeral container. This gives full access to the Node CLI, without having to install it on the host machine.

The command assumes it is run from the root of the project and there is a package.json file present. The -v $(pwd):/app option mounts the current working directory to the /app folder in the container, synchronizing the installed files back to the host directory. The -w /app option sets the work directory of the image as the /app folder. The --loglevel=verbose option causes the output of install command to be verbose. More options can be found on the official Node docker hub page.

docker run --rm -v $(pwd):/app -w /app node npm install --loglevel=verbose

Personally I use a Makefile to store several Ephemeral container commands that are faster to run separate from the build process. But of course, anything is possible :)

Solution 9:[9]

The above-given solutions didn't work for me, I have changed the node image in my from node:alpine to node:12.18.1 and it worked.

Solution 10:[10]

On current latest node:alpine3.13 it's just enough to copy the content of root folder into container's root folder with COPY ./ ./, while omitting the WORKDIR command. But as a practical solution I would recommend:

WORKDIR /usr/app - it goes by convention among developers to put project into separate folder
COPY ./package.json ./ - here we copy only package.json file in order to avoid rebuilds from npm
RUN npm install
COPY ./ ./ - here we copy all the files (remember to create .dockerignore file in the root dir to avoid copying your node_modules folder)

Solution 11:[11]

We also have a similar issue, So I replaced my npm with 'yarn' it worked quit well. here is the sample code.

FROM python:3.7-alpine

ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
#install bash
RUN apk --update add bash zip yaml-dev
RUN apk add --update nodejs yarn build-base postgresql-dev gcc python3- 
dev musl-dev libffi-dev
RUN yarn config set prefix ~/.yarn



#install serverless
RUN yarn global add [email protected] --prefix /usr/local && \
yarn global add [email protected] && \
yarn global add [email protected]


RUN mkdir -p /code
WORKDIR /code

COPY requirements.txt .
COPY requirements-test.txt .

RUN pip install --upgrade pip
RUN pip install -r requirements-test.txt

COPY . .

CMD ["bash"]

Solution 12:[12]

Try npm init and npm install express to create package.json file

Solution 13:[13]

you can to specify node version less than 15.

# Specify a base image
FROM node:14

# Install some dependencies 
COPY ./ ./
RUN npm install

# Default command
CMD ["npm", "start"]