简体   繁体   中英

docker Cannot find module when building image

I'm creating a nodeJS Typescript project and trying to create a docker image of the project. The project successfully compiles on local, but when I try building the docker image (using either docker build or docker-compose up ) it fails spectacularly because it 'Cannot find module x'.

The stranger thing is, when I tried adding RUN npm ls <module> to my dockerfile, it shows that the module was successfully installed. I've seen other questions here that include removing the volume in the docker-compose file, but since mine doesn't even have it to begin with, I'm lost.

I'm trying to do all of this on a Windows machine, if that helps.

Thanks in advance!

docker-compose.yml

version: '3.4'
services:
  app:
    build: .
    ports:
      - "3001:3000"
    image: app-nodejs
    container_name: appnodejs

dockerfile

FROM node:lts-alpine
USER root
ENV PORT 3000
RUN npm install -g pm2 typescript
RUN mkdir -p /app
COPY . /app/
WORKDIR /app
RUN npm install
RUN npm run build
EXPOSE ${PORT}
CMD [ "pm2-runtime", "start", "/app/ecosystem.config.js" ]

Edit: I forgot to mention that it fails at npm run build, but all I'm doing there is running tsc. I can confirm that my tsconfig was properly copied since it returns the 'Cannot find module' error when I try to compile

It turns out the issue was caused by the missing module namely MomentJS https://github.com/moment/moment/issues/5494 After either rolling back or updating to 2.25.1, the issue seems to have been resolved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM