For months, China’s leaders have worked to improve relations with North Korea, its loyal but unpredictable ally to the east. They dispatched a top official to the North for a military parade, rejected calls to isolate it with tough economic sanctions, and spoke glowingly of its leader.

But on Wednesday, Beijing’s hopes of keeping its neighbor in check appeared to diminish, as the North said it had completed a test of a hydrogen bomb about 50 miles from the Chinese border, the fourth nuclear weapon test in a decade.

Officials in Beijing were furious.

“China strongly opposes this act,” Hua Chunying, a spokeswoman for the Chinese Ministry of Foreign Affairs, said in unusually harsh remarks at a news conference on Wednesday. “China will firmly push for denuclearization of the Korean Peninsula.”

The decision to detonate a bomb suggested a serious falling-out in the relationship between North Korea and China. Chinese officials had agreed last fall to send a senior official to the parade in the North Korean capital, Pyongyang — the first such visit since Kim Jong-un, the current leader, took power in 2011 — partly on the condition that the North would refrain from nuclear tests, said Evans J. R. Revere, a former senior State Department specialist on the North.

Now, Chinese leaders are in a difficult position. They are under intense pressure to inflict harsh economic punishments on North Korea, but they worry that any resulting instability could seep back into their territory. They also face new questions about China’s efforts, over the past several months, to curry favor with Mr. Kim, whom many Chinese regard as a bizarre, bumbling figure.

China must also reckon with the prospect that actions by the North could galvanize countries like the United States, Japan and South Korea into strengthening military forces in the Pacific, just as China is seeking to assert its dominance in the region.

“This is precisely what China does not need,” said Rory Medcalf, head of the National Security College at Australian National University. “If there is one issue guaranteed to refocus U.S. strategic attention on Asia, it is a North Korean nuclear provocation, especially one purported to bring Pyongyang’s destructive capabilities to a whole new level.”

On Wednesday, Chinese officials sidestepped questions about whether they would level economic sanctions against North Korea, by paring shipments of oil, for instance, or food. China is North Korea’s largest economic ally, with trade between the two countries having totaled $6.4 billion in 2014.

Cheng Xiaohe, a professor of international relations at Renmin University in Beijing, said China had several ways of increasing economic pressure, including by limiting the number of tourists allowed to visit North Korea or by reducing Chinese imports of commodities like coal.

But he said the leadership in Beijing should be cautious before resorting to more drastic measures.

“If China unilaterally imposes sanctions, it will have no more tricks,” Mr. Cheng said. “Putting more economic pressure on North Korea might also lead to the fall of Kim, the collapse of the regime and all kinds of unpredictable situations China does not wish to see.”

China and North Korea have been allies since the Korean War, when Chinese Communist troops fought to defend the North. Since then, China has helped sustain the rule of the Kim family (Mr. Kim’s father and grandfather governed before him), providing economic aid and political support, even in the face of heavy criticism.

The relationship has become more strained in recent years, as North Korea has pushed forward with its nuclear program in defiance of warnings from the United Nations. After the North said it had carried out a nuclear test in 2013, China’s president, Xi Jinping, warned that world peace should not be threatened by the actions of a single country.

In recent months, Chinese leaders appeared to recalibrate their strategy, seemingly persuaded that Mr. Kim, despite his rash manner, would stay in power much longer than they had anticipated.

In October, a senior Chinese official traveled to Pyongyang to celebrate the 70th anniversary of the founding of Mr. Kim’s governing party. Mr. Xi also sent a personal message, extolling Mr. Kim for “positive progress in developing the economy, improving livelihoods and so on.”

In December, in another apparent gesture of good will, Mr. Kim sent a North Korean pop band to Beijing to perform. But the group canceled at the last minute, suggesting tensions between the countries had resurfaced.

Bonnie S. Glaser, senior adviser for Asia at the Center for Strategic and International Studies in Washington, said the troubles with North Korea would add to a long list of challenges for Mr. Xi, including a slowing economy and rising tensions in the South China Sea.

“Xi doesn’t want instability on the border any more than his predecessors did,” she said. “China will keep North Korea at arm’s length diplomatically, but economic ties will continue to develop.”

Mr. Revere, the former senior State Department employee, said persuading China to take aggressive action against North Korea, including economic sanctions, would be essential to halting the North’s nuclear program.

“China is really the only friend that North Korea has,” he said. “With China, we can at least have some hope of sharpening the regime’s choices. Without China, it undercuts our ability to be effective.”

In China, many people have soured on the Communist Party’s alliance with the North, though some say it is important to have friends in the region.

On Wednesday, many said they were growing anxious about the North’s erratic behavior.

In Yanji, a border city, residents reported feeling tables and chairs shake during the blast from the weapon test, which registered as a magnitude 5.1 seismic event. People flooded online forums, trying to piece together what had happened.

“Will the city be contaminated?” one post said. “Will the radiation harm us? Are we safe?”

By JAVIER HERNANDEZ January 6, 2015 in The New York Times