React.js Docker-找不到模块 [英] React.js Docker - Module not found

查看:98
本文介绍了React.js Docker-找不到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux 18.04

create-react-app 2.0

docker 19.09.0

Dockerfile

Dockerfile

# base image
FROM node:9.6.1

# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH
ADD package.json /package.json

# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install --save --silent
RUN npm install react-scripts@latest -g --silent

# start app
CMD ["npm", "start"]

docker-compose.yml

docker-compose.yml

version: '3.5'

services:

  provisioning-app:
    container_name: prv-app
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - '.:/usr/src/app'
      - '/usr/src/app/node_modules'
    ports:
      - '3000:3000'
    environment:
      - NODE_ENV=development


问题


一切正常,但是在安装了新软件包后,码头工人按预期停止了工作。每次我运行 docker-compose up 都会失败,并显示找不到模块错误。
npm start 正常工作。
我尝试了很多在Web上找到的解决方法:

Problem

Everything has been working just fine, but after installing a new package docker stopped working as expected. Every time I run docker-compose up it fails with 'Module not found' error. While npm start works perfectly fine. I've tried quite a few workarounds found on the web:


  • 重启PC

  • 删除node_modules文件夹并再次运行 npm i

  • npm install的不同组合 / -保存 /包括-在Dockerfile中保存

  • restart PC
  • delete node_modules folder and run npm i again
  • different combinations of npm install / --save / including --save in Dockerfile

它只是行不通,我可以不能自己处理。知道为什么会发生这种情况吗?

It just doesn't work and I can't handle it on my own. Any ideas why this might be happening?

很奇怪,但是此代码有效。我是Docker的新秀,所以我不知道有什么区别

Weirdly, but this code works. I'm a rookie in Docker, so I have no clue what's the difference

docker run -it \
  -v ${PWD}:/usr/src/app \
  -v /usr/src/app/node_modules \
  -p 3000:3000 \
  --rm \
  prov-app


推荐答案


一切都很好,但是在安装了新的
软件包后,docker停止了预期的工作。每次我运行
docker-compose up都会失败,并显示找不到模块错误。

Everything has been working just fine, but after installing a new package docker stopped working as expected. Every time I run docker-compose up it fails with 'Module not found' error.

您需要重建您的映像以安装该软件包。 docker-compose build ,然后 docker-compose up

You need to rebuild your image to have that package installed. docker-compose build, and then docker-compose up

编辑

根据您的更新,我意识到问题在于您的旧node_module卷在两次构建之间仍然存在。尝试 docker-compose down -v ,然后 up

Based on your update, I realize that the problem is your old node_module volume persisted between builds. Try docker-compose down -v before up.

这篇关于React.js Docker-找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆