dagger/dagger · error

failed to set type to module: %w

Error message

failed to set type to module: %w

What it means

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.

Source

Thrown at sdk/typescript/runtime/tsutils/package_json_updator.go:17

package tsutils

import (
	"fmt"
	"typescript-sdk/tsdistconsts"

	"github.com/tidwall/gjson"
	"github.com/tidwall/sjson"
)

func UpdatePackageJSONForModule(packageJSON string) (string, error) {
	packageJSON = removeJSONComments(packageJSON)

	// Set type to module
	packageJSON, err := sjson.Set(packageJSON, "type", "module")
	if err != nil {
		return "", fmt.Errorf("failed to set type to module: %w", err)
	}

	// Set typescript dependency if it's not already set
	packageJSON, err = setIfNotExists(packageJSON, "dependencies.typescript", tsdistconsts.DefaultTypeScriptVersion)
	if err != nil {
		return "", fmt.Errorf("failed to set typescript dependency: %w", err)
	}

	// Remove "@dagger.io/dagger" from dependencies if it's set
	// so we smoothly transition from local to module
	packageJSON, err = sjson.Delete(packageJSON, "dependencies."+gjson.Escape(daggerLibPathAlias))
	if err != nil {
		return "", fmt.Errorf("failed to delete @dagger.io/dagger dependency: %w", err)
	}

	// Remove "@dagger.io/dagger" from devDependencies if it's set
	// so we smoothly transition from local to module
	// (older dagger version were setting dagger in devDependencies)

View on GitHub (pinned to 82ba2681db)

Solutions

  1. Run `npm pkg get type` or jq on package.json to locate the syntax error and fix it.
  2. If package.json is empty or unreadable, delete it and let Dagger regenerate it via `dagger develop`.
  3. Remove JSONC comments; package.json must be strict JSON.
  4. Validate with `npm install --dry-run` after fixing to confirm parseability.

Example fix

// before: trailing comma
{ "name": "my-module", "type": "module", }
// after
{ "name": "my-module", "type": "module" }
Defensive patterns

Strategy: validation

Validate before calling

func validatePackageJSON(pkg string) error {
	if !json.Valid([]byte(pkg)) {
		return errors.New("package.json is not valid JSON")
	}
	return nil
}

Type guard

func isValidPackageJSON(s string) bool {
	return json.Valid([]byte(s))
}

Try / catch

out, err := UpdatePackageJSONForModule(pkgJSON)
if err != nil {
	if strings.Contains(err.Error(), "failed to set type to module") {
		// package.json unparseable; repair or regenerate
	}
	return err
}

Prevention

When it happens

Trigger: CreateOrUpdatePackageJSONForModule (or UpdatePackageJSONForModule directly) invoked with a packageJSON string containing invalid JSON — trailing commas, unquoted keys, truncation, or comments that survived stripping.

Common situations: Hand-edited package.json with a typo; empty package.json; package.json with JSONC-style comments; file corrupted by a crashed tool.

Related errors


AI-assisted analysis of dagger/dagger@82ba2681db (2026-09-05). Data as JSON: /api/errors/6d10e3b29e4a8450. Report an issue: GitHub.