{"id":"0a2af35ec0405acc","repo":"gohugoio/hugo","slug":"npm-pack-could-not-locate-existing-workspaces-arr","errorCode":null,"errorMessage":"npm pack: could not locate existing workspaces array for insertion","messagePattern":"npm pack: could not locate existing workspaces array for insertion","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"modules/npm/package_builder.go","lineNumber":326,"sourceCode":"\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]...)\n\t\t\t\t\tresult = append(result, insertion...)\n\t\t\t\t\tresult = append(result, data[pos:]...)\n\t\t\t\t\treturn afero.WriteFile(fsys, packageJSONName, result, 0o666)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// We know a workspaces array exists but could not locate it reliably in the raw data.\n\t\treturn fmt.Errorf(\"npm pack: could not locate existing workspaces array for insertion\")\n\t}\n\n\t// No workspaces key — add before closing brace.\n\tlastBrace := bytes.LastIndexByte(data, '}')\n\tif lastBrace < 0 {\n\t\treturn fmt.Errorf(\"npm pack: malformed package.json\")\n\t}\n\n\tbefore := bytes.TrimRight(data[:lastBrace], \" \\t\\n\\r\")\n\tneedComma := len(before) > 0 && before[len(before)-1] != '{' && before[len(before)-1] != ','\n\n\tvar insertion string\n\tif needComma {\n\t\tinsertion = \",\\n\" + indent + `\"workspaces\": [` + \"\\n\" + indent + indent + quoted + \"\\n\" + indent + \"]\\n\"\n\t} else {\n\t\tinsertion = indent + `\"workspaces\": [` + \"\\n\" + indent + indent + quoted + \"\\n\" + indent + \"]\\n\"\n\t}\n","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/modules/npm/package_builder.go#L308-L344","documentation":"To preserve the user's package.json formatting, Hugo inserts its workspace path into the existing `workspaces` array by raw byte manipulation: it searches for the literal `\"workspaces\"` key and the following `[`/`]` brackets. If the parsed JSON says a workspaces array exists but that byte-level scan can't find it, Hugo aborts rather than risk corrupting the file.","triggerScenarios":"`hugo mod npm pack` with a package.json that json-parses to an array-form `workspaces`, but where the byte scan fails — e.g. unusual escaping of the key, the first `\"workspaces\"` occurrence in the raw bytes being a different string (a dependency name or nested value appearing before the real key with no `[` after it in an expected position).","commonSituations":"Exotic formatting or minified package.json where another `\"workspaces\"` string literal precedes the actual key; files with unusual whitespace/encoding that defeat the heuristic scan.","solutions":["Reformat package.json conventionally (e.g. `npx prettier --write package.json` or `jq . package.json > tmp && mv tmp package.json`) and re-run.","Manually add the Hugo workspace path (`_hugo_packages` autogen folder shown in the generated config) to the `workspaces` array yourself; Hugo then detects it as already present.","Rename or restructure any other `\"workspaces\"` string literals that appear before the real key."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// the builder inserts entries into an existing workspaces array textually;\n// verify the array literal is present and well-formed\nif bytes.Contains(data, []byte(\"\\\"workspaces\\\"\")) && !regexp.MustCompile(`\"workspaces\"\\s*:\\s*(\\[|\\{)`).Match(data) {\n    return errors.New(\"workspaces key present but not followed by an array/object literal\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep the workspaces array in standard formatting — `\"workspaces\": [ ... ]` — so textual insertion can locate it","Don't put comments-as-strings or unusual whitespace/escaping around the workspaces key","If insertion keeps failing, delete the workspaces field and let hugo mod npm pack create it fresh"],"tags":["hugo","npm-pack","workspaces","formatting"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}