在我的react组件(Coffee.jsx)的顶部,我有这个导入:
import ReactPlayer from 'react-player';
'react-player' 包肯定已安装,出现在package.json
和node_modules/
。
我的代码在docker
容器内运行。每次我旋转我的容器时,就像这样:
docker-compose -f docker-compose-dev.yml up -d
我收到此错误:
./src/components/Coffees.jsx
Module not found: Can't resolve 'react-player' in '/usr/src/app/src/components'
这是控制台向我显示的内容:
Brewing.jsx:22 Uncaught Error: Cannot find module 'react-player'
at webpackMissingModule (Brewing.jsx:22)
at Module../src/components/Coffees.jsx (Brewing.jsx:22)
at __webpack_require__ (bootstrap:781)
at fn (bootstrap:149)
at Module../src/App.jsx (Spotify.css:4)
at __webpack_require__ (bootstrap:781)
at fn (bootstrap:149)
at Module../src/index.js (spotify-auth.js:8)
at __webpack_require__ (bootstrap:781)
at fn (bootstrap:149)
at Object.0 (index.js:10)
at __webpack_require__ (bootstrap:781)
at checkDeferredModules (bootstrap:45)
at Array.webpackJsonpCallback [as push] (bootstrap:32)
at main.chunk.js:1
docker-compose-dev.yml:
client:
build:
context: ./services/client
dockerfile: Dockerfile-dev
volumes:
- './services/client:/usr/src/app'
- '/usr/src/app/node_modules'
ports:
- 3000:3000
environment:
- NODE_ENV=development
- REACT_APP_WEB_SERVICE_URL=${REACT_APP_WEB_SERVICE_URL}
depends_on:
- web
Dockerfile 开发:
# base image
FROM node:11.12.0-alpine
# set working directory
WORKDIR /usr/src/app
# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /usr/src/app/package.json
COPY package-lock.json /usr/src/app/package-lock.json
RUN npm ci
RUN npm install react-scripts@2.1.8 -g --silent
# start app
CMD ["npm", "start"]
文件夹结构:
services/
docker-compose-dev.yml
node_modules/
client/
Dockerfile-dev
package.json
package-lock.json
node_modules/
react-player/
临时修复:
修复这个问题的黑客正在等待一段时间,以及我在Coffee.jsx或Brewing.jsx 中的代码的一些强制更改。
我保存更改后的代码后,找到了包。
然后,当我停止容器并再次启动它们时,问题又来了。我尝试在--build
之后使用标志up -d
,但无济于事。
这是怎么回事?我该如何解决?
更持久的修复:
从 docker-compose-dev.yml 中删除卷并重建后,如下所示:
#volumes:
#- './services/client:/usr/src/app'
#- '/usr/src/app/node_modules'
我仍然收到错误:
client_1 | > client@0.1.0 start /usr/src/app
client_1 | > react-scripts start
client_1 |
client_1 | Could not find a required file.
client_1 | Name: index.html
client_1 | Searched in: /usr/src/app/public
client_1 | npm ERR! code ELIFECYCLE
client_1 | npm ERR! errno 1
client_1 | npm ERR! client@0.1.0 start: `react-scripts start`
client_1 | npm ERR! Exit status 1
client_1 | npm ERR!
client_1 | npm ERR! Failed at the client@0.1.0 start script.
client_1 | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
client_1 |
client_1 | npm ERR! A complete log of this run can be found in:
client_1 | npm ERR! /root/.npm/_logs/2019-11-05T15_14_42_967Z-debug.log
然后它只有在我再次取消注释卷并运行带有卷的容器时才有效。解释原因的答案
a) 临时修复 b) 更永久的修复
将不胜感激。