BEIJING — The United States and China may be involved in a trade war, but the Chinese shoppers at an American wholesaler were at war with one another this week in a pitched battle that forced the warehouse store to close early.

The opening of the first Costco store in China, in the commercial capital of Shanghai, was a scene of chaos. Shoppers lined up for hours to get into the store upon opening, ducking under the roller door as it inched up from the ground.

They fought over Birkin bags and Moutai liquor. They wrestled for detergent and grabbed at a piece of pork with their bare hands, even as the butcher was trying to cut it. There was a three-hour wait for a space in the parking lot — and sometimes longer in checkout lines.

By the afternoon, the discount retail store was in such chaos that it had to close early. “Please don’t come,” Costco said in an alert sent to members, who paid $28 to join for a year.

Even for veterans of Black Friday in the United States, the scenes would have been extraordinary.

Beyond the chaos, they illustrated an important political point. The Chinese and American economies are inextricably intertwined. Chinese shoppers want American products, especially at bargain prices, and American companies want Chinese shoppers.

President Trump a week ago ordered — without the authority or the ability to do so — American companies to leave China.

Chinese state media outlets responded by gleefully pointing out some of the big-name American companies, including Costco, that are doing just the opposite.

The Global Times, a nationalist newspaper that often reflects the foreign policy thinking of the ruling Communist Party, noted that Starbucks and Walmart were expanding, Tesla was set to produce Model 3 cars at its Shanghai factory by the end of the year, and the local head of Coca-Cola said it “must not give up” on China.

“It’s just one beautiful daydream of U.S. President Donald Trump that U.S. companies will give up China,” Liang Ming, a research fellow of the Chinese Academy of International Trade and Economic Cooperation, told the paper.

If the American retail landscape has created winners and losers, the rifts are even more stark between companies that make it in China — and those that don’t.

Home Depot closed its last stores in 2012. Best Buy bought a majority stake in a Chinese electronics chain in 2006 and then withdrew from China altogether eight years later.

Walmart has been in China since 1996 and made steady progress through brick-and-mortar stores and a partnership with the Chinese e-commerce platform

Retail analysts pointed to Costco as a retail darling that has bested the competition, when it comes to international expansion, as a result of the company’s measured and thoughtful approach to every country it enters — and the long-game it plays before officially opening a new store.

Five years ago, Costco partnered with Alibaba — China’s dominant e-commerce platform — which helped the big-box store gain legitimacy among Chinese shoppers, said Mark Cohen, director of retail studies at Columbia Business School. And even after Alibaba gave Costco a platform, Costco took its time, he said, waiting years to prepare for its “grand opening day, where thousands of people showed up.”

“They’ve built a link — a very powerful link — between who they are and what they do, and Chinese consumers who gleefully signed up as subscribers and showed up,” Cohen said.

In a statement, Costco said that when its doors opened in Shanghai, the store “encountered a record-breaking volume of members who wanted to shop with us. We are working closely with local authorities to control traffic flows, and provide smooth transitions to and from our new warehouse.”

Beyond its 543 locations in the United States and Puerto Rico, Costco says it has opened stores in 11 other countries. It has 100 locations in Canada, 39 in Mexico, 29 in the United Kingdom, plus a slew in Taiwan, Japan, Australia and elsewhere. The company has 163,000 full- and part-time employees in the United States and 243,000 worldwide.

But that success isn’t enough to secure a Chinese audience, experts say. Walmart, for example, learned that its model wasn’t replicable everywhere when it had to pull out of Germany 13 years ago. For Costco to maintain such high interest in China, it will have to tailor its selection specifically for this new market.

“Target went to Canada, and it didn’t work,” said Moody’s Vice President Charlie O’Shea. “Canada is literally over the border. Going from the U.S. to China is not going from the U.S. to Canada. There are huge differences over there.”

Unlike retailers that are barely hanging on, O’Shea said that Costco can afford to test a Chinese expansion. Growing slowly doesn’t carry much risk, but there can be a huge payoff from “throwing stuff at the wall and seeing what sticks.”

“They’ve gone in there and picked that site [in Shanghai] with a lot of thought, and look what happened,” he said. “They were mobbed. If anyone can make this a go in that market, it’s Costco.”

Siegel reported from Washington.