To cache the RUN npm install instruction when docker build a Dockerfile with JavaScript, we copy the package.json file to the Docker volume.
For instance, in our Dockerfile, we write
# install node modules
WORKDIR /usr/app
COPY package.json /usr/app/package.json
RUN npm install
# install application
COPY . /usr/app
to add
COPY package.json /usr/app/package.json
to copy package.json from the project to /usr/app/package.json
in the Docker volumne.
Then npm install
the packages that isn’t already installed in the volumne.