Categories
JavaScript Answers

How to fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment?

Spread the love

Sometimes, we want to fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment.

In this article, we’ll look at how to fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment.

How to fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment?

To fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment, we need to install our app’s dependency in the container.

To do this, we write

FROM node:boron

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080
CMD [ "npm", "start" ]

in our Dockerfile for creating our app’s container.

We copy package.json to our container with COPY package.json /usr/src/app/.

And we install the packages with RUN npm install.

Conclusion

To fix ‘Cannot find module’ error for a Node.js app running in a Docker compose environment, we need to install our app’s dependency in the container.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *