我从头开始创建一个新的ReactTs项目,使用Yarn 4.4.1(刚刚开始使用yarn 4)。
我使用 Create React App Client 创建了项目:
# In my /frontend directory
yarn create react-app . --template typescript
当使用
yarn start
本地运行时,它就像一个魅力(嗯,不是真的。我遇到了一些与 /frontend/src/App.test.tsx
文件相关的错误,但这与我的实际问题无关,所以我只是将其注释掉现在 -> 编辑:这里已经解决了#13585)。
编辑: 我尝试使用 npm 和 yarn 1 而不是 yarn 4 并且它可以工作,即使在 docker 容器内也是如此。
但是当我使用 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"]
对 Yarn 4(以及一般来说,Yarn Berry)进行更多调查,我找到了问题所在,这主要是因为我对 Yarn Berry PnP 工作原理的误解:
问题是 Docker 容器内的 Node 无法解析由 Yarn 的 Plug'n'Play (PnP) 管理的包依赖项。 Yarn 用于映射依赖项的
.pnp.cjs
文件(由 CRA 客户端生成)并未自动加载。
我在 Dockerfile 或 docker-compose.yml 中添加了以下环境变量:
# ...
ENV NODE_OPTIONS="--require ./.pnp.cjs" # Add this
CMD ["yarn", "start"]
NODE_OPTIONS
环境变量确保节点(在容器内)在启动时加载.pnp.cjs
文件,使其能够使用Yarn PnP正确解决依赖关系。