You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React项目执行yarn/npm i后未生成node_modules问题求助

问题解决:Ubuntu下Yarn 3安装依赖无node_modules且启动报错

问题现象

  • Windows上正常运行的React项目克隆到Ubuntu 22.04后,执行yarn安装依赖仅出现包弃用警告,无严重错误,但未生成node_modules文件夹
  • 执行yarn start时报错:
yarn start
Internal Error: my-proj@workspace:.: This package doesn't seem to be present in your lockfile; run "yarn install" to update the lockfile
    at V0.getCandidates (/home/hamed/.cache/node/corepack/yarn/3.6.0/yarn.js:435:5145)
    at kf.getCandidates (/home/hamed/.cache/node/corepack/yarn/3.6.0/yarn.js:391:1264)
    at /home/hamed/.cache/node/corepack/yarn/3.6.0/yarn.js:439:8033
    at df (/home/hamed/.cache/node/corepack/yarn/3.6.0/yarn.js:390:11070)
    at ge (/home/hamed/.cache/node/corepack/yarn/3.6.0/yarn.js:439:8013)
  • 环境信息:node 14.21.3,npm 6.14.18,yarn 3.6.0,项目根目录存在yarn.lock

原因分析

Yarn 3默认启用Plug'n'Play (PnP) 模式,该模式不生成node_modules,依赖通过Yarn缓存管理。另外,Windows环境生成的yarn.lock可能与Linux存在兼容性问题,导致依赖解析失败。

解决方案

方案1:禁用PnP模式,生成node_modules

  1. 在项目根目录创建或修改.yarnrc.yml文件,添加配置:
nodeLinker: node-modules
  1. 删除原有lock文件并清理缓存:
rm yarn.lock
yarn cache clean
  1. 重新安装依赖:
yarn install

完成后会生成node_modules,执行yarn start即可启动项目。

方案2:修复PnP模式下的lockfile兼容性

若想保留PnP模式,执行以下步骤:

  1. 删除原有yarn.lock:
rm yarn.lock
  1. 重新生成适配Linux环境的lockfile:
yarn install
  1. 执行启动命令:
yarn start

方案3:降级Yarn版本(适配项目原环境)

如果项目在Windows上原本使用Yarn 1.x,可降级到对应版本:

  1. 禁用当前Corepack管理的Yarn:
corepack disable yarn
  1. 安装Yarn 1.x:
npm install -g yarn@1.x
  1. 删除旧lock文件后重新安装:
rm yarn.lock
yarn install

内容的提问来源于stack exchange,提问作者hsbr13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 18:12:23