When an npm workspace package has the same name as an external npm dependency, npm becomes confused about whether to resolve the package locally or from the registry, causing installation failures. Renaming your workspace package or using scoped names prevents this conflict.
Fixes EINVALIDWORKSPACE
{
"workspaces": [
"packages/ui",
"packages/api",
"packages/express"
]
}{"workspaces": ["packages/ui","packages/api","packages/express"]}npm ERR! code EINVALIDWORKSPACEnpm ERR! Workspace name conflicts with dependency name
In npm workspaces (monorepo setups), npm resolves packages by their configured package.json 'name' field, not by directory. When a workspace package has the same name as an external dependency, npm's dependency resolver cannot distinguish between the local workspace package and the registry package, leading to resolution failures.
Look at your root package.json and identify all packages:
{
"workspaces": [
"packages/ui",
"packages/api",
"packages/express"
]
}Then check the name field in each workspace's package.json.
Compare your workspace package names with your dependencies:
for dir in packages/*/; do
cat "$dir/package.json" | grep '"name"'
doneIf a workspace name matches a dependency name (e.g., both 'express'), you've found the conflict.
Use scoped package names for all workspace packages:
In packages/express/package.json, change:
{
"name": "express"
}To:
{
"name": "@myorg/express"
}Update references throughout your monorepo:
{
"dependencies": {
"@myorg/express": "workspace:*"
}
}Update imports in your code:
// Before
import something from 'express';
// After
import something from '@myorg/express';Clean up and reinstall:
rm -rf node_modules package-lock.json
npm cache clean --force
npm installThis error is particularly common when migrating legacy monorepos to npm workspaces. The root cause is that npm's workspace resolution is name-based (from package.json), not directory-based. Using scoped packages (@org/name) is the npm community standard for avoiding these collisions. For organizations, establishing a consistent scoping convention early prevents this issue.