node_modules is the local project directory containing installed Node.js packages and their dependencies. Because it can be large, keep it out of Git and recreate it with npm install. section 2.3.2 section 2.2
Manage the directory through the project’s dependency files:
- Use package.json to declare the project and its dependencies. section 2.2
- Use package-lock.json to preserve detailed dependency information and support consistent builds. section 2.2 section 5.4.2
- Delete node_modules when necessary and restore it with npm install. section 2.3.2 section 2.2
- Keep node_modules at the project root when containerizing, because moving it is not recommended. section 5.4.2
Listing 5.13 Node.js Dockerfile
# Using the official Node.js image for the LTS version FROM node:18.17.0 # Set our app's working directory WORKDIR /app # copy the primary source code COPY ./src /app/src/ # copy the package.json and package-lock.json to # ensure we use the same dependencies COPY package.json /app/ COPY package-lock.json /app/ # copy the entrypoint.sh for running the app COPY conf/entrypoint.sh /app/ # install dependencies RUN npm install # make the entrypoint.sh executable RUN chmod +x entrypoint.sh # run our app CMD ["./entrypoint.sh"]
A Node.js Dockerfile copies package.json and package-lock.json, then runs npm install to create node_modules inside /app. section 5.4.2
