5

The Node.js docker tutorial (https://nodejs.org/en/docs/guides/nodejs-docker-webapp/) specifies that the npm install should be run on the host prior to starting docker to generate the package-lock.json file.

How should this file be generated when npm/node are not available on the host?

How should the package-lock.json get updated when new dependencies are added to the package.json?

npm specifies that the package-lock.json file should be checked in to source control. When npm install is run via docker, it generates the package-lock.json file in the container - which is not where it would be checked out from source control. The obvious workaround would be to copy the file from the container to the host whenever it is updated but that seems like there should be an easier solution.

Eddie
  • 61
  • 1
  • 4
  • Npm install would be run in the container, as part of the docker build. – user1751825 Sep 11 '18 at 23:04
  • Possible duplicate of [How do I npm update dependency versions in the package-lock.json?](https://stackoverflow.com/questions/51083789/how-do-i-npm-update-dependency-versions-in-the-package-lock-json) – David Maze Sep 11 '18 at 23:45
  • npm install running in the container generates the file in the container - is it standard practice to copy the file to the host to check it into source control? – Eddie Sep 12 '18 at 02:41
  • It is definitely standard practice to check the lock file into source control (in every package manager I've used that has the concept, in every language). – David Maze Sep 12 '18 at 10:27

1 Answers1

4

I usually just create a temporary container to run npm inside instead of having to install node and npm on the host. Something like this:

docker run --rm -v "$(pwd)":/data -w /data -it node bash

and then inside bash I run npm init to generate a package.json and npm install to generate package-lock.json. You may want to use -u "$UID" to make the file be owned by your host user too, or just chown it after.

I do the same thing to install new packages, just npm install package inside bash on the temporary container.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • You can also try using `docker run --rm -v "$(pwd)":/data -w /data -it 'node:alpine' sh` to use lightweight alpine instead of debian based container. – wpedrak Jan 18 '19 at 22:21
  • What does the -w do? I can't seem to find it in the documentation. – srchulo Jan 09 '20 at 03:27
  • 1
    @srchulo It sets the current directory (inside the container) for the command you're running – Paul Jan 09 '20 at 04:00
  • @Paul thanks! I thought maybe that was it, but I removed it and I was in the same directory. Maybe that's because I set the same WORKDIR in my Dockerfile. – srchulo Jan 10 '20 at 02:15