Updated:

How /enchant works

/enchant @p minecraft:sharpness 5

It enchants the item in the target's main hand. Java and Bedrock write it almost the same way.

Why it fails

On Java, /give with components goes past these limits; see the strongest sword. Bedrock commands cannot exceed them.

Going past the maximum on Java

# Java 1.21.5 and later
/give @p minecraft:diamond_sword[enchantments={sharpness:10}]
# Java 1.20.5 to 1.21.4
/give @p minecraft:diamond_sword[enchantments={levels:{sharpness:10}}]

/give writes the enchantment straight onto a new item, so the level cap and item checks do not apply. The enchant command generator builds this for your version.

Enchantments that cannot be combined

GroupOnly one of
DamageSharpness, Smite, Bane of Arthropods (and Density, Breach on a mace)
ProtectionProtection, Fire, Blast and Projectile Protection
BowInfinity or Mending
MiningSilk Touch or Fortune
BootsDepth Strider or Frost Walker
TridentRiptide or Loyalty, and Riptide or Channeling
CrossbowMultishot or Piercing

/enchant refuses these pairs. A Java /give with components can still put both on one item.

Weapons

EnchantmentIDMax
SharpnesssharpnessV
SmitesmiteV
Bane of Arthropodsbane_of_arthropodsV
KnockbackknockbackII
Fire Aspectfire_aspectII
LootinglootingIII
Sweeping Edgesweeping_edgeIII (Java)
PowerpowerV
PunchpunchII
FlameflameI
InfinityinfinityI
MultishotmultishotI
Quick Chargequick_chargeIII
PiercingpiercingIV
LoyaltyloyaltyIII
ImpalingimpalingV
RiptideriptideIII
ChannelingchannelingI
Density (mace)densityV
Breach (mace)breachIV
Wind Burst (mace)wind_burstIII

The three mace enchantments arrived with the mace in 1.21.

Armor

EnchantmentIDMax
ProtectionprotectionIV
Fire Protectionfire_protectionIV
Blast Protectionblast_protectionIV
Projectile Protectionprojectile_protectionIV
Feather Fallingfeather_fallingIV
RespirationrespirationIII
Aqua Affinityaqua_affinityI
ThornsthornsIII
Depth Striderdepth_striderIII
Frost Walkerfrost_walkerII
Soul Speedsoul_speedIII
Swift Sneakswift_sneakIII

Tools and all items

EnchantmentIDMax
EfficiencyefficiencyV
Silk Touchsilk_touchI
FortunefortuneIII
Luck of the Sealuck_of_the_seaIII
LurelureIII
UnbreakingunbreakingIII
MendingmendingI
Curse of Bindingbinding_curseI
Curse of Vanishingvanishing_curseI

Common questions

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.