hyperledger/fabric · error

--targetPeer must be specified for channel-less operation us

Error message

--targetPeer must be specified for channel-less operation using connection profile

What it means

When using a connection profile for channel-less lifecycle operations, the CLI cannot infer which peer to contact from the profile's channel section, so it requires --targetPeer to name a peer defined in the profile. This error is returned when no channel ID is given and no target peer is specified.

Source

Thrown at internal/peer/lifecycle/chaincode/client_connections.go:156

	if len(input.TLSRootCertFiles) != len(input.PeerAddresses) {
		return errors.Errorf("number of peer addresses (%d) does not match the number of TLS root cert files (%d)", len(input.PeerAddresses), len(input.TLSRootCertFiles))
	}

	return nil
}

func (c *ClientConnectionsInput) parseConnectionProfile() error {
	networkConfig, err := common.GetConfig(c.ConnectionProfilePath)
	if err != nil {
		return err
	}

	c.PeerAddresses = []string{}
	c.TLSRootCertFiles = []string{}

	if c.ChannelID == "" {
		if c.TargetPeer == "" {
			return errors.New("--targetPeer must be specified for channel-less operation using connection profile")
		}
		return c.appendPeerConfig(networkConfig, c.TargetPeer)
	}

	if len(networkConfig.Channels[c.ChannelID].Peers) == 0 {
		return nil
	}

	for peer, peerChannelConfig := range networkConfig.Channels[c.ChannelID].Peers {
		if peerChannelConfig.EndorsingPeer {
			err := c.appendPeerConfig(networkConfig, peer)
			if err != nil {
				return err
			}
		}
	}

	return nil

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Add --targetPeer <peerName> matching a key under "peers" in the connection profile
  2. Or pass --channelID so peers can be resolved from the profile's channel section
  3. Or skip the connection profile and use explicit --peerAddresses/--tlsRootCertFiles

Example fix

// before
peer lifecycle chaincode queryinstalled --connectionProfile cp.yaml
// after
peer lifecycle chaincode queryinstalled --connectionProfile cp.yaml --targetPeer peer0.org1.example.com:7051
Defensive patterns

Strategy: validation

Validate before calling

if connectionProfile != "" && channelID == "" && targetPeer == "" { return errors.New("--targetPeer required with connection profile and no channel") }

Try / catch

if err := run(cmd); err != nil && strings.Contains(err.Error(), "--targetPeer must be specified") { log.Fatal("add --targetPeer or --channelID") }

Prevention

When it happens

Trigger: Running a lifecycle command with --connectionProfile, no --channelID, and no --targetPeer; the code path clears PeerAddresses and requires TargetPeer before appendPeerConfig is called.

Common situations: Omitting --channelID for commands that don't need one (like queryinstalled) while relying on a connection profile without naming the peer; profile peer names not matching keys used by --targetPeer.

Related errors


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