I am trying to create a node.js docker container for an api layer transpiled from typescript copied from a scr folder to a dist folder. The node.js container seems to exit immediately and when I issue docker-compose logs I get "Error: Cannot find module '/usr/src/app/webapp.js'", however, when I bring up the container with the -d option, I can see where the file and directory structure is in the correct hierarchical structure. What could be wrong here? Why is node not able to locate the webapp.js file?
Dockerfile:
FROM node:latest
# install our dependencies and nodejs
RUN mkdir -p /usr/src
RUN mkdir -p /tmp/dist
ADD Account/package.json /tmp/package.json
RUN cd /tmp && npm install --production
ADD Account/dist /tmp/dist
RUN mkdir -p /tmp/node_modules/mongodb-repository
ADD Account/node_modules/mongodb-repository /tmp/node_modules/mongodb-repository
RUN mkdir -p /usr/src/app && cp -a /tmp/node_modules /usr/src/app && cp -a /tmp/package-lock.json /usr/src/app
RUN cp -a /tmp/dist/application /usr/src/app && cp -a /tmp/dist/config /usr/src/app && cp -a /tmp/dist/domain /usr/src/app && cp -a /tmp/dist/infrastructure /usr/src/app
RUN cp -a /tmp/dist/routes /usr/src/app && cp -a /tmp/dist/types /usr/src/app && cp -a /tmp/dist/webapp.js /usr/src/app && cp -a /tmp/package.json /usr/src/app
RUN chmod 755 /usr/src/app/webapp.js
RUN mkdir -p /usr/src/app/bin
COPY Account/bin /usr/src/app/bin
COPY Account/.env /usr/src/app/.env
WORKDIR /usr/src/app
CMD [ "node", "webapp.js" ]
EXPOSE 3000
docker-compose.yml
version: '2'
services:
mongo:
container_name: "app_mongo"
tty: true
image: mongo:latest
environment:
- MONGO_DATA_DIR=/data/db
- MONGO_LOG_DIR=/dev/null
- MONGO_INITDB_ROOT_USERNAME=********
- MONGO_INITDB_ROOT_PASSWORD=********
volumes:
- /data/dbDocker:/data/db
ports:
- 27017:27017
command: "mongod --smallfiles --auth"
rabbitmq:
container_name: "app_rabbitmq"
tty: true
image: rabbitmq:management
ports:
- "15672:15672"
- "15671:15671"
- "5672:5672"
volumes:
- /rabbitmq/lib:/var/lib/rabbitmq
- /rabbitmq/log:/var/log/rabbitmq
- /rabbitmq/conf:/etc/rabbitmq/
group:
container_name: "app_group"
build:
context: .
dockerfile: ./Account/Dockerfile
volumes:
- ./Account:/usr/src/app/
ports:
- "3000:3000"
depends_on:
- mongo
- rabbitmq
To fix the Cannot find module error, simply install the missing modules using npm . This will install the project's dependencies into your project so that you can use them. Sometimes, this might still not resolve it for you. In this case, you'll want to just delete your node_modules folder and lock file ( package-lock.
The "Cannot find module" error in Node. js occurs for multiple reasons: Forgetting to install a third-party package with npm i somePackage . Pointing the node command to a file that doesn't exist. Having an outdated version of the package, or an IDE or development server glitch.
To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node . You can then import path with the following line of code import * as path from 'path' .
During your image build, you install some things into /usr/src/app
, and then you set it as the working directory. Here are the lines from Dockerfile where that happens:
RUN mkdir -p /usr/src
RUN mkdir -p /usr/src/app && cp -a /tmp/node_modules /usr/src/app && cp -a /tmp/package-lock.json /usr/src/app
RUN cp -a /tmp/dist/application /usr/src/app && cp -a /tmp/dist/config /usr/src/app && cp -a /tmp/dist/domain /usr/src/app && cp -a /tmp/dist/infrastructure /usr/src/app
RUN cp -a /tmp/dist/routes /usr/src/app && cp -a /tmp/dist/types /usr/src/app && cp -a /tmp/dist/webapp.js /usr/src/app && cp -a /tmp/package.json /usr/src/app
RUN chmod 755 /usr/src/app/webapp.js
RUN mkdir -p /usr/src/app/bin
COPY Account/bin /usr/src/app/bin
COPY Account/.env /usr/src/app/.env
WORKDIR /usr/src/app
After the build phase, all of that is baked into your image and ready to be used. But at runtime, you told docker-compose:
volumes:
- ./Account:/usr/src/app/
This is an overlay mount. Whatever is built into the image at /usr/src/app
is completely ignored and replaced by the contents of ./Account
from the directory where docker-compose.yml is located.
I don't know enough about your project to tell you how to properly fix this, but that's where your error is probably coming from. All the work done during build to construct /usr/src/app
is being undone by mounting another directory on top of it at runtime.
If you remove that volume mount, all of /usr/src/app
is still there and ready to use. But that may have other side effects that you will need to account for to make your app do its job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With