BACKGROUND: Cannabis use disorder is associated with considerable comorbidity and impairment in functioning, and prevalence is increasing among adults with chronic pain. We aimed to assess the effect of introduction of medical cannabis laws (MCL) and recreational cannabis laws (RCL) on the increase in cannabis use disorder among patients in the US Veterans Health Administration (VHA). METHODS: Data from patients with one or more primary care, emergency, or mental health visit to the VHA in 2005-19 were analysed using 15 repeated cross-sectional VHA electronic health record datasets (ie, one dataset per year). Patients in hospice or palliative care were excluded. Patients were stratified as having chronic pain or not using an American Pain Society taxonomy of painful medical conditions. We used staggered-adoption difference-in-difference analyses to estimate the role of MCL and RCL enactment in the increases in prevalence of diagnosed cannabis use disorder and associations with presence of chronic pain, accounting for the year that state laws were enacted. We did this by fitting a linear binomial regression model stratified by pain, with time-varying cannabis law status, fixed effects for state, categorical year, time-varying state-level sociodemographic covariates, and patient covariates (age group [18-34 years, 35-64 years, and 65-75 years], sex, and race and ethnicity). FINDINGS: Between 2005 and 2019, 3 234 382-4 579 994 patients were included per year. Among patients without pain in 2005, 5·1% were female, mean age was 58·3 (SD 12·6) years, and 75·7%, 15·6%, and 3·6% were White, Black, and Hispanic or Latino, respectively. In 2019, 9·3% were female, mean age was 56·7 (SD 15·2) years, and 68·1%, 18·2%, and 6·5% were White, Black, and Hispanic or Latino, respectively. Among patients with pain in 2005, 7·1% were female, mean age was 57·2 (SD 11·4) years, and 74·0%, 17·8%, and 3·9% were White, Black, and Hispanic or Latino, respectively. In 2019, 12·4% were female, mean age was 57·2 (SD 13·8) years, and 65·3%, 21·9%, and 7·0% were White, Black, and Hispanic or Latino, respectively. Among patients with chronic pain, enacting MCL led to a 0·135% (95% CI 0·118-0·153) absolute increase in cannabis use disorder prevalence, with 8·4% of the total increase in MCL-enacting states attributable to MCL. Enacting RCL led to a 0·188% (0·160-0·217) absolute increase in cannabis use disorder prevalence, with 11·5% of the total increase in RCL-enacting states attributable to RCL. In patients without chronic pain, enacting MCL and RCL led to smaller absolute increases in cannabis use disorder prevalence (MCL: 0·037% [0·027-0·048], 5·7% attributable to MCL; RCL: 0·042% [0·023-0·060], 6·0% attributable to RCL). Overall, associations of MCL and RCL with cannabis use disorder were greater in patients with chronic pain than in patients without chronic pain. INTERPRETATION: Increasing cannabis use disorder prevalence among patients with chronic pain following state legalisation is a public health concern, especially among older age groups. Given cannabis commercialisation and widespread public beliefs about its efficacy, clinical monitoring of cannabis use and discussion of the risk of cannabis use disorder among patients with chronic pain is warranted. FUNDING: NIDA grant R01DA048860, New York State Psychiatric Institute, and the VA Centers of Excellence in Substance Addiction Treatment and Education.