How to fix npm ERR! code EUNKNOWNWORKSPACE npm ERR! Unknown workspace: "package-name" in npm
This error occurs when npm cannot find a workspace with the name you specified using the --workspace flag. It typically happens due to package name mismatches between package.json and folder structure.
Quick fix
Fixes npm ERR! code EUNKNOWNWORKSPACE npm ERR! Unknown workspace: "package-name"
{
"name": "my-monorepo",
"workspaces": [
"packages/*"
]
}- Verify workspace configuration in root package.json
- Check the package.json name field in each workspace
- Use the correct workspace name in your commands
- List all available workspaces to verify names
{"name": "my-monorepo","workspaces": ["packages/*"]}npm ERR! code EUNKNOWNWORKSPACEnpm ERR! Unknown workspace: "package-name"
What this error means
The EUNKNOWNWORKSPACE error occurs in npm monorepo projects when you attempt to run commands with the --workspace (-w) flag, but npm cannot locate a workspace matching that name. npm identifies workspaces by the 'name' field in each workspace's package.json file, not by the folder name. If the workspace name you specified doesn't match any configured workspace's package name, npm raises this error. This is a common confusion point because developers often expect workspaces to be identified by their directory names rather than their package.json names.
Typical symptoms
- Running 'npm run <script> -w <name>' fails with 'Unknown workspace' error
- Workspace commands work sometimes but not others depending on how you reference the workspace
- Error message includes the exact package name npm couldn't find in the workspace filter set
- Commands work at the root but fail when trying to target a specific workspace
- Confusion between using folder names vs package names when running workspace commands
Common causes
- Using the folder name instead of the package name from package.json (most common)
- Mismatch between the folder name and the 'name' field in the workspace's package.json
- Typos in the workspace name or package name reference
- Workspace not listed in the root package.json's 'workspaces' glob patterns
- Using scoped package names incorrectly or forgetting the scope prefix
How to fix "npm ERR! code EUNKNOWNWORKSPACE npm ERR! Unknown workspace: "package-name""
Open your project's root package.json and check the 'workspaces' field to confirm it includes the directory pattern containing your workspace.
{
"name": "my-monorepo",
"workspaces": [
"packages/*"
]
}Make sure the glob pattern matches your actual folder structure.
Open the package.json file inside the workspace directory and verify the 'name' field. This is the identifier npm uses.
{
"name": "@myapp/frontend",
"version": "1.0.0"
}If the workspace folder is 'packages/frontend', but the name is '@myapp/frontend', you must use '@myapp/frontend' when referencing it in npm commands.
Replace your command with the correct workspace package name. Use the 'name' field from the workspace's package.json, not the folder name.
# Incorrect - uses folder name
npm run build -w frontend
# Correct - uses package name from package.json
npm run build -w @myapp/frontendYou can also run commands in all workspaces by omitting the -w flag entirely: npm run build
Run npm with the --workspaces flag to see all configured workspaces and their actual package names:
npm run --workspaces
# Or use npm list to see all workspaces:
npm list --workspacesThis will show you the exact package names npm recognizes for your workspaces.
If you've recently changed workspace configuration or package names, clear npm's cache and reinstall dependencies:
npm cache clean --force
rm package-lock.json
npm installThis ensures npm's internal workspace registry is updated with the correct workspace names.
Advanced notes
When upgrading to npm 7+, some developers encounter workspace issues due to breaking changes in workspace handling. If you're migrating from npm 6 or earlier, ensure all workspace packages have unique names and are properly referenced. Additionally, be aware that some npm commands don't support workspaces and require the --no-workspaces flag. If you're using the 'workspace:' protocol (common in pnpm), you'll need to use pnpm instead of npm, or replace 'workspace:*' dependencies with specific version numbers.
Frequently asked questions
- What does "npm ERR! code EUNKNOWNWORKSPACE npm ERR! Unknown workspace: "package-n" mean?
- The EUNKNOWNWORKSPACE error occurs in npm monorepo projects when you attempt to run commands with the --workspace (-w) flag, but npm cannot locate a workspace matching that name. json file, not by the folder name. If the workspace name you specified doesn't match any configured workspace's package name, npm raises this error.
- How do I fix "npm ERR! code EUNKNOWNWORKSPACE npm ERR! Unknown workspace: "package-n"?
- This error occurs when npm cannot find a workspace with the name you specified using the --workspace flag. It typically happens due to package name mismatches between package.json and folder structure.
- What causes "npm ERR! code EUNKNOWNWORKSPACE npm ERR! Unknown workspace: "package-n"?
- Using the folder name instead of the package name from package.json (most common) Mismatch between the folder name and the 'name' field in the workspace's package.json Typos in the workspace name or package name reference
- Is the "npm ERR! code EUNKNOWNWORKSPACE npm ERR! Unknown workspace: "package-n" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐npm Official Documentation
- ๐Docs: npm workspaces(Official Docs)
- ๐ฌSO: npm workspaces(Stack Overflow)
- ๐GitHub: npm issues(GitHub)