使用Yarn 4在Docker中运行ReactTs项目时缺失react-scripts包问题
React + TypeScript + Yarn 4 Docker运行报错问题
我从零开始搭建了一个基于React TypeScript的项目,使用Yarn 4.4.1(首次接触Yarn 4)。通过Create React App初始化项目,执行命令如下:
# 在我的/frontend目录下 yarn create react-app . --template typescript
本地执行yarn start基本正常(之前遇到过和/frontend/src/App.test.tsx相关的错误,但和当前问题无关,暂时注释掉了——编辑:该问题已在#13585中解决)。
编辑补充:我尝试用npm和Yarn 1替代Yarn 4,在Docker容器中均可正常运行。
但使用Yarn 4在Docker中运行时,遇到以下错误:
Attaching to web-1 web-1 | web-1 | node:internal/modules/run_main:129 web-1 | triggerUncaughtException( web-1 | ^ web-1 | Error: Required package missing from disk. If you keep your packages inside your repository then restarting the Node process may be enough. Otherwise, try to run an install first. web-1 | web-1 | Missing package: react-scripts@virtual:d9650954c7f1725ea712d2e40695de9ea5d1c0fa9b89379134c62909d440d91b03d6c37e2823804a3404472b90a5bec73c82193190dd3c4db4c9e9edd75db91e#npm:5.0.1 web-1 | Expected package location: /app/.yarn/__virtual__/react-scripts-virtual-f2aa641dfb/5/.yarn/berry/cache/react-scripts-npm-5.0.1-d06bd2d5ad-10c0.zip/node_modules/react-scripts/ web-1 | web-1 | at makeError (/app/.pnp.cjs:23863:34) web-1 | at resolveUnqualified (/app/.pnp.cjs:25584:17) web-1 | at resolveRequest (/app/.pnp.cjs:25635:14) web-1 | at Object.resolveRequest (/app/.pnp.cjs:25691:26) web-1 | at resolve$1 (file:///app/.pnp.loader.mjs:2043:21) web-1 | at nextResolve (node:internal/modules/esm/hooks:866:28) web-1 | at Hooks.resolve (node:internal/modules/esm/hooks:304:30) web-1 | at MessagePort.handleMessage (node:internal/modules/esm/worker:196:24) web-1 | at [nodejs.internal.kHybridDispatch] (node:internal/event_target:820:20) web-1 | at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28) web-1 | web-1 | Node.js v20.17.0 web-1 exited with code 1
我通过以下命令检查容器:
docker compose run --rm web yarn info react-scripts
结果显示react-scripts已安装,但在容器内执行yarn start时仍出现相同错误。
我的compose.yml文件内容如下:
services: web: build: context: ./frontend dockerfile: Dockerfile.development ports: - 3000:3000 stdin_open: true volumes: - type: bind source: ./frontend target: /app - type: volume source: frontend.node_modules target: /app/node_modules volumes: frontend.node_modules:
frontend/Dockerfile.development文件内容如下:
FROM node:20 WORKDIR /app RUN corepack enable && corepack prepare yarn@stable --activate COPY . . RUN yarn install --immutable CMD ["yarn", "start"]
内容的提问来源于stack exchange,提问作者Bravo2bad
相关产品推荐
相关产品推荐

