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.
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.
npm ERR! code E401 npm ERR! 401 Unauthorized - Token has expired
Token has expired - npm authentication failure
npm ERR! code EAI_NODATA npm ERR! errno EAI_NODATA npm ERR! getaddrinfo EAI_NODATA registry.npmjs.org
How to fix "npm ERR! code EAI_NODATA - getaddrinfo EAI_NODATA"
npm ERR! code EMPTYPACKAGE npm ERR! Package contains no files
How to fix 'npm ERR! code EMPTYPACKAGE' - Package contains no files
npm ERR! code EWORKSPACEMISSING npm ERR! Workspace does not exist: packages/missing
How to fix "npm ERR! code EWORKSPACEMISSING - Workspace does not exist" error
npm ERR! code EADDRNOTAVAIL npm ERR! errno EADDRNOTAVAIL npm ERR! Address not available
How to fix "npm ERR! code EADDRNOTAVAIL - Address not available" error