This error occurs when the workspaces field in package.json is malformed. Usually caused by using an object instead of array, invalid glob patterns, or workspace packages missing their package.json.
The EINVALIDWORKSPACE error means npm found a `workspaces` field but couldn't parse it correctly. npm requires workspaces to be configured as an array of paths or glob patterns. Common configuration mistakes include: - Using an object format (which Yarn accepts but npm doesn't) - Invalid glob syntax - Empty workspaces array - Workspace directories that exist but don't contain valid package.json files This error prevents npm from initializing the workspace structure entirely.
npm requires workspaces to be an array, not an object:
// ❌ Wrong (Yarn format, not supported by npm)
{
"workspaces": {
"packages": ["packages/*"]
}
}
// ✅ Correct (npm format)
{
"workspaces": [
"packages/*"
]
}Ensure your package.json is valid JSON:
# Check for JSON syntax errors
cat package.json | python -m json.tool
# Or use npm's built-in check
npm pkg get workspacesCommon JSON errors:
- Trailing commas
- Missing quotes around strings
- Unescaped special characters
Every directory matched by your glob must contain a package.json:
# List directories that would match
ls -d packages/*
# Check each has package.json
for dir in packages/*/; do
if [ ! -f "$dir/package.json" ]; then
echo "Missing: $dir/package.json"
fi
doneCreate missing package.json files:
npm init -w ./packages/missing-packageEnsure glob patterns are valid:
{
"workspaces": [
"packages/*", // All direct children of packages/
"apps/*/", // Trailing slash is optional
"tools/tool-*" // Wildcards in names work
]
}Invalid patterns:
- packages/**/* - recursive globs may not work as expected
- Empty string ""
- Paths with unmatched brackets
Check if there's a conflicting global setting:
# View all npm config
npm config list
# Check for workspaces setting
npm config get workspaces
# Remove if set globally
npm config delete workspaces --location=userYarn vs npm workspaces: Yarn accepts object format with nohoist options:
// Yarn format (NOT supported by npm)
{
"workspaces": {
"packages": ["packages/*"],
"nohoist": ["**/react-native"]
}
}If migrating from Yarn, convert to array format.
npm version requirements: Workspaces require npm 7+. Check with npm --version and upgrade if needed.
Workspace package requirements: Each workspace package.json must have at least:
{
"name": "package-name",
"version": "1.0.0"
}Debug workspace detection:
npm query ':root > .workspace'This shows all detected workspaces.
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