{"id":"838649995811c264","repo":"gohugoio/hugo","slug":"npm-pack-unsupported-workspaces-type-t","errorCode":null,"errorMessage":"npm pack: unsupported workspaces type %T","messagePattern":"npm pack: unsupported workspaces type %T","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"modules/npm/package_builder.go","lineNumber":299,"sourceCode":"\n\t\t// Append the new workspace path to the packages slice.\n\t\tnewPkgs := make([]any, 0, len(packagesSlice)+1)\n\t\tfor _, s := range packagesSlice {\n\t\t\tnewPkgs = append(newPkgs, s)\n\t\t}\n\t\tnewPkgs = append(newPkgs, workspacePath)\n\t\tv[\"packages\"] = newPkgs\n\n\t\tupdated, err := json.MarshalIndent(pkg, \"\", indent)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"npm pack: failed to marshal package.json with updated workspaces: %w\", err)\n\t\t}\n\t\tupdated = append(updated, '\\n')\n\t\treturn afero.WriteFile(fsys, packageJSONName, updated, 0o666)\n\tcase nil:\n\t\t// Treat explicit null as \"no workspaces\"; handled below as missing key.\n\tdefault:\n\t\treturn fmt.Errorf(\"npm pack: unsupported workspaces type %T\", v)\n\t}\n\n\t// Try adding to existing workspaces array when present.\n\tif hasWS && wsVal != nil {\n\t\tif wsIdx := bytes.Index(data, []byte(`\"workspaces\"`)); wsIdx >= 0 {\n\t\t\trest := data[wsIdx:]\n\t\t\tif bracketOpen := bytes.IndexByte(rest, '['); bracketOpen >= 0 {\n\t\t\t\tif bracketClose := bytes.IndexByte(rest[bracketOpen:], ']'); bracketClose >= 0 {\n\t\t\t\t\tpos := wsIdx + bracketOpen + bracketClose\n\t\t\t\t\tarrayContent := bytes.TrimSpace(data[wsIdx+bracketOpen+1 : pos])\n\t\t\t\t\tvar insertion string\n\t\t\t\t\tif len(arrayContent) == 0 {\n\t\t\t\t\t\tinsertion = \"\\n\" + indent + indent + quoted + \"\\n\" + indent\n\t\t\t\t\t} else {\n\t\t\t\t\t\tinsertion = \",\\n\" + indent + indent + quoted + \"\\n\" + indent\n\t\t\t\t\t}\n\t\t\t\t\tresult := make([]byte, 0, len(data)+len(insertion))\n\t\t\t\t\tresult = append(result, data[:pos]...)","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/modules/npm/package_builder.go#L281-L317","documentation":"The `workspaces` field in the root package.json may be an array, an object with `packages`, or null/absent. Any other JSON type — a string, number, or boolean — is invalid per npm's schema, and Hugo rejects it with the concrete Go type (%T) rather than guessing an interpretation.","triggerScenarios":"`hugo mod npm pack` reading a root package.json where `workspaces` is e.g. `\"packages/*\"` (a bare string), `true`, or a number — anything other than []any, map[string]any, or null.","commonSituations":"Writing `\"workspaces\": \"packages/*\"` instead of an array (a frequent hand-editing mistake); a templating/codegen step emitting the wrong type; copying a glob from docs without the surrounding brackets.","solutions":["Wrap the value in an array: `\"workspaces\": [\"packages/*\"]`.","If you intended the yarn object form, use `\"workspaces\": { \"packages\": [\"packages/*\"] }`.","Validate against npm's package.json schema before re-running `hugo mod npm pack`."],"exampleFix":"// before\n{ \"workspaces\": \"packages/*\" }\n// after\n{ \"workspaces\": [\"packages/*\"] }","handlingStrategy":"type-guard","validationCode":"switch ws := pkg[\"workspaces\"].(type) {\ncase []any, map[string]any, nil:\n    // supported\ndefault:\n    return fmt.Errorf(\"workspaces must be an array or object, got %T\", ws)\n}","typeGuard":"func isValidWorkspaces(v any) bool {\n    switch v.(type) {\n    case nil, []any, map[string]any:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["\"workspaces\" must be a JSON array of globs or an object with \"packages\" — never a string, number, or bool","A common mistake is \"workspaces\": \"packages/*\" (bare string) — wrap it in an array","Schema-validate package.json (e.g. against the npm package.json schema) in pre-commit"],"tags":["hugo","npm-pack","workspaces","package-json","type-error"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}