RICHMOND, Va. — Voters in Virginia and New Jersey gave Democratic gubernatorial candidates large victories Tuesday and sent a clear message of rebuke to President Donald Trump.

In Virginia’s hard-fought contest, Democratic Lt. Gov. Ralph Northam defeated Republican Ed Gillespie. In New Jersey front-running Democrat Phil Murphy overcame Republican Lt. Gov. Kim Guadagno to succeed unpopular GOP Gov. Chris Christie.

The wins in Virginia and New Jersey are a morale boost to Democrats who had so far been unable to channel anti-Trump energy into success at the ballot box in a major election this year.

Roseann Petropoulos said her opposition to Trump led her out in the cold rain to cast her ballot for Murphy in New Jersey.

“Trump was huge,” she said. “Because back in the ’70s when we got civil rights for people, women’s rights, we have a chance for that to be reversed. It’s scary.”

Northam rode to victory in part by tapping into voters’ regret at Trump’s victory in last year’s national election. Murphy had an easier path in New Jersey, where Guadagno contended with both Trump’s and Christie’s unpopularity.

In New York City, Democrat Bill de Blasio cruised to a second term as mayor.

De Blasio easily defeated Republican state lawmaker Nicole Malliotakis and several third-party candidates Tuesday.

In Boston, Mayor Marty Walsh won a second four-year term by beating City Councilor Tito Jackson after a low-key campaign. And in Maine, voters expanded Medicaid in a statewide referendum that was seen as bellwether for support for the Affordable Care Act.

Northam, Virginia’s lieutenant governor, repeatedly tried to tie Gillespie to the president. His victory was in large part due to a surge in anti-Trump energy since the president took office. Democrats said they had record levels of enthusiasm heading into the race in Virginia, a swing-state and the only Southern state that Trump lost last year.

Gillespie kept Trump at a distance throughout the campaign but tried to rally the president’s supporters with hard-edge attack ads focused on illegal immigration and preserving Confederate statues. The strategy was criticized by Democrats and some Republicans as race baiting.

After Tuesday’s loss though, Trump suggested that Gillespie hurt himself by not more closely aligning himself with the president.

“Ed Gillespie worked hard but did not embrace me or what I stand for,” Trump tweeted , before pointing out that Republicans have won every special election to the U.S. House since he was elected.

In Virginia, Northam’s victory is another sign of the state’s shift toward a more liberal electorate. Democrats have won every statewide election since 2009 and now have won four out of the last five gubernatorial contests.

Early returns also showed Democrats unseating at least four Republican incumbents, including a woman poised to defeat one of the most ardently anti-LGBT rights members of the General Assembly to become Virginia’s first openly transgender lawmaker.

A pediatric neurologist and Army doctor, Northam made health care reform a centerpiece of his campaign, winning key allies along the way.

He was a leading opponent of a Republican effort to mandate ultrasounds before abortions in 2012, winning him strong support from well-funded abortion-rights groups.

Northam’s victory is a blow to Republicans, who were hoping that Gillespie could provide a possible roadmap for moderate Republicans to follow in next year’s midterm elections.

Washington Post contributed.