liveBook · Manning

Node_modules

As explained in Road to Kubernetes

The node_modules directory is the local filesystem directory containing installed Node.js packages and their dependencies.

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

How Road to Kubernetes covers it

How deep it goes
Explains contents, deletion, recovery, tracking files, and container placement, but does not examine package internals.
Examples
Uses npm install, a Node.js .gitignore entry, and a Dockerfile that installs dependencies under /app.
What it assumes
Assumes a Node.js project using npm, package.json, and package-lock.json to manage dependencies.
What you can do after
Operate/configure Node.js dependencies by ignoring, deleting, restoring, and containerizing node_modules with npm.
How it connects
Connects node_modules to npm, package.json, package-lock.json, Git, and Dockerfile workflows.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.