hyperledger/fabric · error
chaincode %s exists
Error message
chaincode %s exists
What it means
PutChaincodeToFS writes the chaincode package to chaincodeInstallPath/<name>.<version> and refuses to overwrite an existing installation. If os.Stat finds the target file already present, this error is returned so an existing chaincode package is never silently replaced.
Source
Thrown at core/common/ccprovider/sigcdspackage.go:311
if ccpack.env == nil {
return errors.New("env cannot be nil if buf and depspec are not nil")
}
if ccpack.data == nil {
return errors.New("nil data")
}
if ccpack.datab == nil {
return errors.New("nil data bytes")
}
ccname := ccpack.depSpec.ChaincodeSpec.ChaincodeId.Name
ccversion := ccpack.depSpec.ChaincodeSpec.ChaincodeId.Version
// return error if chaincode exists
path := fmt.Sprintf("%s/%s.%s", chaincodeInstallPath, ccname, ccversion)
if _, err := os.Stat(path); err == nil {
return fmt.Errorf("chaincode %s exists", path)
}
if err := os.WriteFile(path, ccpack.buf, 0o644); err != nil {
return err
}
return nil
}
View on GitHub (pinned to 2736b63f8f)
Solutions
- Verify the chaincode with `peer chaincode list --installed`; if already present, skip the install step
- Bump the chaincode version and package a new version before reinstalling
- Delete the stale package file at the reported path (or wipe the peer's chaincode install directory) only if the existing package is no longer needed, then retry
Example fix
// before peer chaincode install cc.out # version 1.0, already installed -> error // after # bump version and package again, or guard in script: if ! peer chaincode list --installed | grep -q 'mycc@1.0'; then peer chaincode install cc.out fi
Defensive patterns
Strategy: try-catch
Validate before calling
path := filepath.Join(chaincodeInstallPath, ccname+"."+ccversion)
if _, err := os.Stat(path); err == nil {
return fmt.Errorf("chaincode %s already installed; skip or bump version", path)
} Type guard
func chaincodeInstalled(installPath, name, version string) bool {
_, err := os.Stat(filepath.Join(installPath, name+"."+version))
return err == nil
} Try / catch
err := ccprovider.PutChaincodeToFS(pack)
if err != nil {
var pathErr *os.PathError
if strings.Contains(err.Error(), "exists") {
log.Printf("already installed, treating as success")
return nil // idempotent install
}
return err
} Prevention
- Check installed chaincode list before each install
- Make install scripts idempotent (skip when name@version exists)
- Always bump the version for each new deployment
When it happens
Trigger: Installing the same chaincode name+version twice on the same peer, e.g., running `peer chaincode install` for an already-installed version, or reinstalling after a failed deployment without changing the version.
Common situations: Re-running an install script idempotently, retrying a failed upgrade while keeping the same version label, or a peer whose filesystem already contains packages from a prior deployment.
Related errors
- chaincode already successfully installed (package ID '%s')
- error writing chaincode install package to %s
- env cannot be nil if buf and depspec are not nil
- nil data
- nil data bytes
AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04).
Data as JSON: /api/errors/ae6bfe637caf2812.
Report an issue: GitHub.