Knicks owner James Dolan’s worst nightmare has unfolded as the crosstown rival Nets swiped Kevin Durant — and Kyrie Irving — from their hands.

While the traded Kristaps Porzingis is expected to sign a long-term deal with the Mavericks, Durant announced on The Boardroom’s Instagram account Sunday that he’s going to Brooklyn to sign a four-year, $164 million deal.

An unhappy Porzingis was traded in late January by the Knicks in a deal that opened max slots for Durant and Irving. But the Nets did their own maneuvering and opened up the same amount of cap space to outdo the Knicks again with a chance to take over the city’s basketball scene.

Irving’s leap to the Nets was expected for two weeks, but Durant’s decision was a slap-in-the-face stunner after many months of inaccurate reports elsewhere stating it was a “done deal” with the Knicks.

Team president Steve Mills released a statement saying the franchise remains “upbeat.’’

“While we understand that some Knicks fans could be disappointed with tonight’s news, we continue to be upbeat and confident in our plans to rebuild the Knicks to compete for championships in the future, through the draft, targeted free agents and continuing to build around our core of young players,” Mills said in the statement.

An ESPN report stated owner James Dolan was not prepared to offer Durant the max because of the Achilles tear he suffered during the NBA Finals. However, sources close to Mills last week said Durant was on their max list, but they’d proceed cautiously and wanted to see his medical records.

“The injury definitely changed things,’’ one source said.

Forget about seeing medical records. The Knicks never even got a chance at an official meeting to give their charismatic coach David Fizdale a chance to sell the Knicks.

“Three months ago, that was a very confident front office,” one NBA GM said. “It’s crazy what happened today.”

The Nets are also set to sign ex-Knicks center DeAndre Jordan, a close friend of Durant’s whom the Knicks had obtained in the Porzingis deal.

In more bad news, the Knicks had interest in point guard Terry Rozier, but he’s signing a three-year, $58 million deal with the Hornets.

The Knicks, with alternate plans to fill their $72 million in cap space with short-term deals, started that process in a frenzy by signing three power forwards — youngsters Julius Randle and Bobby Portis and veteran Taj Gibson — to two-year guarantees. The Knicks were decimated at the position and now don’t have enough cap room for Kawhi Leonard.

While Randle and Portis have upside, the signings likely won’t turn the Knicks into a relevant club next season after missing the playoffs for six straight seasons.

Knicks brass was informed Sunday morning it was out of the Durant chase and flew to Los Angeles for meetings, including with Lakers forward Reggie Bullock. They have $25 million left in cap space and there’s no longer a reason to meet with Leonard.

“They just have to stick to their plan,’’ said one NBA source close to the Knicks. “The team needs some patience to build the team the right way. If you can’t catch a break, you got to stay the course.’’

Mills and GM Scott Perry have said for months a new culture/vibe had star players wanting to be with the Knicks. But they have struck out on Durant, Irving and Kemba Walker, and their jobs could be in jeopardy.

The Knicks, coming off a league-worst 17-65 season, have young prospects from the past three drafts and will need a big rookie year from No. 3 pick RJ Barrett and a stout jump from 2018 lottery pick Kevin Knox to become relevant again.

Durant will miss next season with his Achilles tear, but Irving will join a club that made the playoffs last season. Durant will be 32 when he returns from his injury.

One NBA source said the Knicks weren’t 100 percent sold on Irving as a solo man if he didn’t also come with Durant. The Knicks had some concerns about Irving’s leadership after the Celtics’ internal strife that was blamed on Irving.

Perhaps if Durant didn’t get hurt, things could have broken differently, with Irving more willing to play for the Knicks with the former Warriors star on his side and not on the sidelines.

In February, Dolan boasted big things would happen this summer.

“Look, New York is the mecca of basketball. We hear from people all the time, from players and representatives about who wants to come,” Dolan said on “The Michael Kay Show” on ESPN radio. “We can’t respond because of the NBA rules, etc. But that doesn’t stop them from telling us, and they do. I can tell you, from what we’ve heard, I think we’re gonna have a very successful offseason when it comes to free agents.”

So far, that only applies to the Nets.