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

通过Yarn Workspaces配置多工作区遇依赖报错的问题排查

Fixing Yarn Workspace Error: Couldn't find package "@app/module-a" on npm registry

Let's break down what's going wrong and walk through how to fix it:

First, Simplify Your Monorepo Structure

Your current setup has redundant nested workspace configurations that are confusing Yarn. Here's a cleaned-up view of your structure:

demo-app/
├── package.json (root, with workspaces: ["module-core/packages/*","src/packages/*"])
├── src/
│   ├── package.json (unnecessary workspaces config)
│   └── packages/
│       ├── module-a/
│       │   └── package.json (name: @app/module-a, private: true, unnecessary workspaces config)
│       └── module-b/
│           └── package.json (name: @app/module-b, private: true, unnecessary workspaces config)
└── module-core/
    ├── package.json (unnecessary workspaces config)
    └── packages/
        ├── core-a/
        │   └── package.json (name: @app/core-a, private: true, unnecessary workspaces config)
        └── core-b/
            └── package.json (name: @app/core-b, private: true, unnecessary workspaces config)

Why You're Seeing the Error

Yarn is trying to fetch @app/module-a from the npm registry instead of recognizing it as a local workspace package. This happens for two key reasons:

  1. Nested workspace configurations are redundant: Only the root package.json needs to define workspaces. Subdirectories like src/, module-a/, etc., aren't workspace roots—adding workspaces fields there messes with Yarn's package resolution logic.
  2. Dependency versions might not point to local workspaces: If module-a references @app/core-a with a fixed version like ^0.0.0, Yarn might default to checking the registry instead of using your local copy.

Step-by-Step Fixes

1. Remove Redundant Workspace Configurations

Delete the workspaces field from these files entirely:

  • src/package.json
  • module-core/package.json
  • All sub-package package.json files (module-a/package.json, module-b/package.json, core-a/package.json, core-b/package.json)

Your root package.json already includes all the necessary paths for Yarn to find your local packages.

2. Update Dependencies to Use Workspace References

In module-a/package.json, update the dependency for @app/core-a to use the workspace:* alias—this explicitly tells Yarn to use the local workspace version:

"dependencies": {
  "@app/core-a": "workspace:*"
}

Do the same for module-b/package.json with @app/core-b:

"dependencies": {
  "@app/core-b": "workspace:*"
}

3. Reinstall Dependencies Cleanly

First, clear existing artifacts to avoid caching issues:

rm -rf node_modules yarn.lock

Then run the install command again:

yarn install

4. Verify Workspace Recognition

To confirm Yarn is correctly identifying all your local packages, run:

yarn workspaces list

You should see all four packages (@app/module-a, @app/module-b, @app/core-a, @app/core-b) listed—this means Yarn now recognizes them as part of the workspace.

Why This Works

Yarn's workspace system is built to be managed from a single root. Removing nested configs eliminates conflicting resolution rules, and using workspace:* ensures Yarn prioritizes your local packages over remote registry lookups—fixing the "couldn't find package" error entirely.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:53:21