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

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

问题描述

Linux 18.04

创建反应应用 2.0

docker 19.09.0

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 停止按预期工作.每次我运行 docker-compose up 时,它都会因找不到模块"错误而失败.虽然 npm start 工作得很好.我已经尝试了很多在网上找到的解决方法:

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:

  • 重启电脑
  • 删除node_modules文件夹并再次运行npm i
  • npm install/--save 的不同组合/在Dockerfile中包含--save
  • 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 卷在构建之间持续存在.在 up 之前尝试 docker-compose down -v.

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天全站免登陆