How to fix npm ERR! code EINVALIDWORKSPACE npm ERR! Invalid workspace configuration in npm
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.
Quick fix
Fixes npm ERR! code EINVALIDWORKSPACE npm ERR! Invalid workspace configuration
// ❌ Wrong (Yarn format, not supported by npm)
{
"workspaces": {
"packages": ["packages/*"]
}
}
// ✅ Correct (npm format)
{
"workspaces": [
"packages/*"
]
}- Use array format for workspaces
- Validate package.json syntax
- Verify each workspace has package.json
- Check glob pattern syntax
// ❌ Wrong (Yarn format, not supported by npm){"workspaces": {"packages": ["packages/*"]}}// ✅ Correct (npm format){"workspaces": ["packages/*"]}npm ERR! code EINVALIDWORKSPACEnpm ERR! Invalid workspace configuration
What this error means
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.
Typical symptoms
- npm install fails with 'Invalid workspace configuration'
- EINVALIDWORKSPACE error code
- Error occurs before any packages are installed
- Worked with Yarn but fails with npm
Common causes
- Using object format instead of array for workspaces
- Invalid glob pattern syntax
- Workspace directory missing package.json
- Empty or null workspaces field
- Corrupted or invalid JSON in root package.json
How to fix "npm ERR! code EINVALIDWORKSPACE npm ERR! Invalid workspace configuration"
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=userAdvanced notes
Yarn 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.
Frequently asked questions
- What does "npm ERR! code EINVALIDWORKSPACE npm ERR! Invalid workspace configurati" mean?
- 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. json files This error prevents npm from initializing the workspace structure entirely.
- How do I fix "npm ERR! code EINVALIDWORKSPACE npm ERR! Invalid workspace configurati"?
- 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.
- What causes "npm ERR! code EINVALIDWORKSPACE npm ERR! Invalid workspace configurati"?
- Using object format instead of array for workspaces Invalid glob pattern syntax Workspace directory missing package.json
- Is the "npm ERR! code EINVALIDWORKSPACE npm ERR! Invalid workspace configurati" 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
- 📖npm Docs: Workspaces Configuration(Official Docs)
- 🐙
- 💬Netlify: npm workspace errors(Stack Overflow)