hyperledger/fabric · error

Error writing Chaincode package contents: %s

Error message

Error writing Chaincode package contents: %s

What it means

While packaging the node.js project, util.WriteFolderToTarPackage failed to write the source folder's contents into the tar writer. The platform logs the underlying error and wraps it as 'Error writing Chaincode package contents'. This is a packaging/I/O failure, not a code validation failure.

Source

Thrown at core/chaincode/platforms/node/platform.go:151

	payload := bytes.NewBuffer(nil)
	gw := gzip.NewWriter(payload)
	tw := tar.NewWriter(gw)

	folder := path
	if folder == "" {
		return nil, errors.New("ChaincodeSpec's path cannot be empty")
	}

	// trim trailing slash if it exists
	if folder[len(folder)-1] == '/' {
		folder = folder[:len(folder)-1]
	}

	logger.Debugf("Packaging node.js project from path %s", folder)

	if err = util.WriteFolderToTarPackage(tw, folder, []string{"node_modules"}, nil, nil); err != nil {
		logger.Errorf("Error writing folder to tar package %s", err)
		return nil, fmt.Errorf("Error writing Chaincode package contents: %s", err)
	}

	// Write the tar file out
	if err := tw.Close(); err != nil {
		return nil, fmt.Errorf("Error writing Chaincode package contents: %s", err)
	}

	tw.Close()
	gw.Close()

	return payload.Bytes(), nil
}

func (p *Platform) GenerateDockerfile() (string, error) {
	var buf []string

	buf = append(buf, "FROM "+util.GetDockerImageFromConfig("chaincode.node.runtime"))
	buf = append(buf, "ADD binpackage.tar /usr/local/src")

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Check the wrapped underlying error in the message/ logs; fix the root cause it reports.
  2. Verify the ChaincodeSpec path exists and is readable (ls <path>).
  3. Re-run packaging after restoring missing/unreadable source files.
  4. Exclude problematic files (node_modules is already skipped) and retry the package command.

Example fix

// before
peer chaincode package -p ./nonexistent-dir -n cc -l node
// after
peer chaincode package -p ./correct-chaincode-dir -n cc -l node
Defensive patterns

Strategy: try-catch

Validate before calling

const fs = require('fs');
if (!fs.existsSync(chaincodePath) || !fs.statSync(chaincodePath).isDirectory()) {
  throw new Error(`Chaincode path ${chaincodePath} does not exist or is not a directory`);
}

Try / catch

try {
  payload = getDeploymentPayload(spec);
} catch (err) {
  if (/Error writing Chaincode package contents/.test(err.message)) {
    // check source path exists/readable, fix permissions, then retry once
  }
  throw err;
}

Prevention

When it happens

Trigger: GetDeploymentPayload called with a folder whose contents cannot be walked or written to the tar stream — missing source directory, unreadable files, or util.WriteFolderToTarPackage returning an error (it excludes 'node_modules').

Common situations: Chaincode path points to a nonexistent or misspelled directory, permission errors on source files, disk or stream failures during tar/gzip writes, or packaging from a path where files were deleted mid-build.

Related errors


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/ee352fcc088d7cb7. Report an issue: GitHub.