{"record":{"id":"6d10e3b29e4a8450","repo":"dagger/dagger","slug":"failed-to-set-type-to-module-w","errorCode":null,"errorMessage":"failed to set type to module: %w","messagePattern":"failed to set type to module: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdk/typescript/runtime/tsutils/package_json_updator.go","lineNumber":17,"sourceCode":"package tsutils\n\nimport (\n\t\"fmt\"\n\t\"typescript-sdk/tsdistconsts\"\n\n\t\"github.com/tidwall/gjson\"\n\t\"github.com/tidwall/sjson\"\n)\n\nfunc UpdatePackageJSONForModule(packageJSON string) (string, error) {\n\tpackageJSON = removeJSONComments(packageJSON)\n\n\t// Set type to module\n\tpackageJSON, err := sjson.Set(packageJSON, \"type\", \"module\")\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to set type to module: %w\", err)\n\t}\n\n\t// Set typescript dependency if it's not already set\n\tpackageJSON, err = setIfNotExists(packageJSON, \"dependencies.typescript\", tsdistconsts.DefaultTypeScriptVersion)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to set typescript dependency: %w\", err)\n\t}\n\n\t// Remove \"@dagger.io/dagger\" from dependencies if it's set\n\t// so we smoothly transition from local to module\n\tpackageJSON, err = sjson.Delete(packageJSON, \"dependencies.\"+gjson.Escape(daggerLibPathAlias))\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to delete @dagger.io/dagger dependency: %w\", err)\n\t}\n\n\t// Remove \"@dagger.io/dagger\" from devDependencies if it's set\n\t// so we smoothly transition from local to module\n\t// (older dagger version were setting dagger in devDependencies)","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/dagger/dagger/blob/82ba2681dbe30d3547a1dc50ea495900ab5b6047/sdk/typescript/runtime/tsutils/package_json_updator.go#L1-L35","documentation":"UpdatePackageJSONForModule patches a module's package.json with sjson.Set(packageJSON, \"type\", \"module\") so the SDK runs as an ES module. This error fires when sjson.Set fails, which happens when the package.json contents are not valid JSON after removeJSONComments processing — Dagger cannot safely rewrite the file, so codegen aborts.","triggerScenarios":"CreateOrUpdatePackageJSONForModule (or UpdatePackageJSONForModule directly) invoked with a packageJSON string containing invalid JSON — trailing commas, unquoted keys, truncation, or comments that survived stripping.","commonSituations":"Hand-edited package.json with a typo; empty package.json; package.json with JSONC-style comments; file corrupted by a crashed tool.","solutions":["Run `npm pkg get type` or jq on package.json to locate the syntax error and fix it.","If package.json is empty or unreadable, delete it and let Dagger regenerate it via `dagger develop`.","Remove JSONC comments; package.json must be strict JSON.","Validate with `npm install --dry-run` after fixing to confirm parseability."],"exampleFix":"// before: trailing comma\n{ \"name\": \"my-module\", \"type\": \"module\", }\n// after\n{ \"name\": \"my-module\", \"type\": \"module\" }","handlingStrategy":"validation","validationCode":"func validatePackageJSON(pkg string) error {\n\tif !json.Valid([]byte(pkg)) {\n\t\treturn errors.New(\"package.json is not valid JSON\")\n\t}\n\treturn nil\n}","typeGuard":"func isValidPackageJSON(s string) bool {\n\treturn json.Valid([]byte(s))\n}","tryCatchPattern":"out, err := UpdatePackageJSONForModule(pkgJSON)\nif err != nil {\n\tif strings.Contains(err.Error(), \"failed to set type to module\") {\n\t\t// package.json unparseable; repair or regenerate\n\t}\n\treturn err\n}","preventionTips":["Use `npm pkg set`/npm CLI for package.json edits instead of manual editing.","Never put comments in package.json (strict JSON only).","Run `npm pkg get` or jq in CI before dagger develop.","Keep files free of BOM characters."],"tags":["typescript","npm","json","config"],"backgroundTag":"malformed-json-config","analyzedSha":"82ba2681dbe30d3547a1dc50ea495900ab5b6047","analyzedAt":"2026-09-05T07:21:37.930Z","contentChangedAt":"2026-09-05T07:21:37.930Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}