President Trump got slammed by all sides for suddenly escalating his economic war with China on Friday. But for Americans who have lost a family member or friend to the Chinese-made street drug fentanyl, Trump’s harsh pivot is the right move.

He hiked tariffs on Chinese goods, labeled President Xi an “enemy” and told US companies manufacturing in China to pull out. Wall Street went into a tailspin, and partisans called Trump “unhinged.”

By Monday, Trump changed his tone, in response to friendly overtures from Beijing. But the reality is that harsh words and economic threats are what’s needed. China has been waging chemical narcotic war against Americans for several years, flooding our neighborhoods with poison.

In the last three years alone, fentanyl and similar synthetic opioids manufactured in Chinese labs have killed some 79,000 Americans. That’s more than the American combatants killed in Vietnam, Afghanistan and Iraq combined. And like those combatants, most fentanyl victims are young.

Chinese-made narcotics started showing up on our streets in 2013, according to the Centers for Disease Control and Prevention. Each year, overdose deaths soared, but the Obama administration never confronted China to stop its deadly assault.

Trump is fighting back. Instead of soldiers and bullets, he is using tariffs and other economic sanctions. How civilized of him. “We’re losing thousands of people to fentanyl,” Trump tweeted. “This is more important than anything else that we’re working on.”

Trump explained on Friday that China’s refusal to curb the flood of fentanyl is a major reason for the tariff hikes. Outraged by the tens of thousands of deaths caused by Chinese-made fentanyl, Trump tweeted “President Xi said this would stop — it didn’t.”

In fact, China is refusing to ­cooperate with US law enforcement. Federal authorities have ­indicted three Chinese drug kingpins who manufacture fentanyl and fentanyl ingredients and market the lethal drugs over the internet to Americans. China won’t ­extradite the accused to America and instead is allowing them to continue to operate freely.

Trump’s critics, like Bryce Pardo of RAND Corporation, claim getting tough with China is futile, because China doesn’t have the ­inspectors and law enforcement to shut the poison factories. That’s nonsense. A country with the brutal totalitarian apparatus to enforce a one-child policy — which China did for years, dictating what went on in the bedroom — can control what gets sold on the internet and put in the mail. China’s synthetic opioid factories typically employ hundreds of people seated at internet terminals, openly selling their lethal wares.

The Obama administration, and many Democrats even now, insist the right strategy is to fund drug-treatment programs and expand Medicaid, curbing the demand for killer street drugs instead of cutting off the supply.

Actually, we need to do both — attack the supplier and treat the addicts. In war, you don’t stop fighting the enemy while you are bandaging the wounded.

For years, the United States Postal Service has been the Chinese drug dealers’ shipping method of choice. The feds did nothing until Congress passed a law in 2018 requiring that every package from China be labeled with the content and origin. Still, only about 100 of the 1.3 million ­international packages coming in every day actually get inspected by Customs and Border Protection. Friday, Trump tweeted: “Ordering all carriers to SEARCH FOR AND REFUSE all deliveries of fentanyl from China.” A daunting task.

Some of the Chinese fentanyl supplies are actually shipped to Mexico, then smuggled across the southern border to the United States. Inspecting packages at that border for fentanyl, a congressional report concluded, is like “finding a needle in a haystack.”

Trump knows that stopping the drugs on our border is less likely to succeed than stopping China from sending them.

Meanwhile, Monday morning Trump announced that Chinese officials had called their US counterparts Sunday night and said, “Let’s get back to the table.”

Will Trump’s hard-line stance on trade stop the drug massacre on America’s streets? It’s possible. Trump said Monday morning, ­reflecting on China’s change of tone, that the Chinese “understand how life works.”

Betsy McCaughey is a former lieutenant governor of New York.