{"record":{"id":"67cf6f2d0b50e8a6","repo":"pulumi/pulumi","slug":"unmarshal-package-json-w-67cf6f","errorCode":null,"errorMessage":"unmarshal package.json: %w","messagePattern":"unmarshal package\\.json: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdk/nodejs/cmd/pulumi-language-nodejs/main.go","lineNumber":1974,"sourceCode":"\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"could not write output file %s: %w\", filename, err)\n\t\t}\n\t}\n\n\treturn &pulumirpc.GeneratePackageResponse{\n\t\tDiagnostics: rpcDiagnostics,\n\t}, nil\n}\n\nfunc readPackageJSON(packageJSONPath string) (map[string]any, error) {\n\tpackageJSONData, err := os.ReadFile(packageJSONPath)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"read package.json: %w\", err)\n\t}\n\tvar packageJSON map[string]any\n\terr = json.Unmarshal(packageJSONData, &packageJSON)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unmarshal package.json: %w\", err)\n\t}\n\treturn packageJSON, nil\n}\n\nfunc (host *nodeLanguageHost) Pack(ctx context.Context, req *pulumirpc.PackRequest) (*pulumirpc.PackResponse, error) {\n\t// Verify npm exists and is set up: npm, user login\n\tnpm, err := executable.FindExecutable(\"npm\")\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"find npm: %w\", err)\n\t}\n\n\t// Annoyingly the engine will call Pack for the core SDK which is not setup in at all the same way as the\n\t// generated sdks, so we have to detect that and do a big branch to pack it totally differently.\n\tpackageJSON, err := readPackageJSON(filepath.Join(req.PackageDirectory, \"package.json\"))\n\tif err != nil {\n\t\treturn nil, err\n\t}\n","sourceCodeStart":1956,"sourceCodeEnd":1992,"githubUrl":"https://github.com/pulumi/pulumi/blob/793f7b2e160db4321fb7fb6b0607461e01cb251e/sdk/nodejs/cmd/pulumi-language-nodejs/main.go#L1956-L1992","documentation":"The Node.js language host reads package.json from the package directory and unmarshals it with json.Unmarshal in readPackageJSON (sdk/nodejs/cmd/pulumi-language-nodejs/main.go:1974). If the file exists but is not valid JSON (or is not a JSON object), the host wraps the unmarshal error with \"unmarshal package.json: %w\" and fails the Pack call.","triggerScenarios":"Pulumi calls Pack on a package whose <PackageDirectory>/package.json exists but fails JSON parsing: truncated file, comments or trailing commas, BOM, invalid escape sequences, or a top-level JSON array/number/string instead of an object.","commonSituations":"Hand-edited package.json left syntactically invalid; a merge conflict marker committed into package.json; a file generated by a tool that wrote partial output; CI building a corrupted checkout.","solutions":["Open <packageDirectory>/package.json and fix the JSON syntax error reported by the wrapped message (line/column are included by encoding/json)","Validate the file with 'node -e \"JSON.parse(require(\\\"fs\\\").readFileSync(\\\"package.json\\\"))\"' or 'npx jsonlint package.json'","If the file was edited by hand, regenerate it with 'npm init' or the tooling that originally produced it","Re-run the pulumi pack/pack-sdk command from a clean checkout"],"exampleFix":"// before (package.json)\n{\n  \"name\": \"my-provider\",\n  \"version\": \"1.0.0\", // trailing comment\n}\n// after\n{\n  \"name\": \"my-provider\",\n  \"version\": \"1.0.0\"\n}","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nif (!fs.existsSync('package.json')) throw new Error('package.json missing');\nJSON.parse(fs.readFileSync('package.json', 'utf8')); // throws with position if invalid","typeGuard":"function isValidPackageJSON(raw) {\n  try {\n    const parsed = JSON.parse(raw);\n    return typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed);\n  } catch { return false; }\n}","tryCatchPattern":"try {\n  JSON.parse(fs.readFileSync('package.json', 'utf8'));\n} catch (err) {\n  console.error('package.json is not valid JSON:', err.message);\n  process.exit(1);\n}","preventionTips":["Run a JSON linter on package.json in pre-commit hooks","Never hand-edit package.json while merge conflicts are unresolved","Use npm/node tooling to modify package.json instead of manual edits","Add 'node -e \"JSON.parse(...)\"' as a CI sanity check"],"tags":["json","nodejs","package-json","parsing"],"backgroundTag":"invalid-json","analyzedSha":"793f7b2e160db4321fb7fb6b0607461e01cb251e","analyzedAt":"2026-08-31T09:36:43.099Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}