By 7 a.m., lines of customers snake down the block outside stores on the main commercial strip in Musina, a bustling South African border town where thousands of people arrive daily from neighboring Zimbabwe to buy food, clothes and other necessities that are hard to get back home.

A few miles away, at the border, pickup trucks bearing the seal of South Africa’s newly formed border patrol inspect the razor-wire fence, looking to arrest people who cross illegally — braving bandits, crocodiles and the rushing Limpopo River. The border force represents an effort by the government, months ahead of crucial national elections, to respond to popular demand and clamp down on migrants sneaking into the country.

Musina, surrounded by farms and a copper mine, is where the government’s muscular immigration policy collides with a tricky reality that many South Africans are loath to concede: that even people who cross the border illegally may be good for the country.

Without them, “Musina is going to be a big ghost town,” said Jan-Pierre Vivier, a South African who, with his family, owns a butcher shop that relies on migrant customers.

Like politicians in the United States, Europe and elsewhere who score points by promising hardened borders and mass deportation, their South African counterparts are pitching a sweeping crackdown on foreigners to appeal to voters, playing on similar, often-unfounded fears that immigrants fuel crime and steal jobs.

South Africa has its own struggles with poverty and extreme inequality, but it is wealthy compared with some of its neighbors, making it a tempting destination for migrants from Africa and beyond.

Last month, South Africa’s government proposed the most sweeping overhaul of its immigration laws since becoming a democracy in 1994, aiming to greatly restrict the entry of foreigners. In October, President Cyril Ramaphosa officially launched the new border patrol agency to coordinate police, military and treasury operations, saying that an increase in undocumented migration had “exacerbated many of the country’s social and economic problems.”

Early this month, in an effort to show how tough the new border agency has been, its leader said it had stopped 443 Zimbabwean children traveling on 42 buses without their parents from being “trafficked” into South Africa at the border post near Musina.

Zimbabwean officials quickly rejected the claim as fiction, saying they had no record of the South African authorities handing over that many children. Zimbabweans living in South Africa said that even if busloads of children had been stopped, they were not being trafficked, but instead were coming into South Africa to visit their parents for the holidays, a typical practice.

“Everything goes back to, we are going to elections,” said Yona Zhoya, a native of Zimbabwe who lives in South Africa and works with immigrants. “As soon as you say, ‘Down with foreigners,’ then you get mileage or you get their votes.”

As anti-immigrant violence has flared in parts of South Africa, Mr. Zhoya said that many migrants were so fearful that they were sending valuables back to their home countries, worried that their homes might be attacked.

A survey showed that last year, 69 percent of South Africans believed that immigrants increased crime.

But in Musina, locals are more than happy to look the other way when Zimbabweans wade across the Limpopo, sneak through holes in the border fence, or grease a guard’s palm.

Business owners in Musina do not feel like they are competing with foreign migrants, as they might be in some of South Africa’s big cities, said Moses Matshiva, who owns a building that houses a tavern, pharmacy and hookah bar in Nancefield, a township near Musina.

“We here don’t complain because they come and buy and go back,” he said.

Shopkeepers cater to their cross-border customers by adjusting their operating hours to accommodate people who have traveled overnight and by selling items in bulk, like trays of canned food, buckets of cookies and crates of energy drinks.

Mr. Vivier’s butcher shop on Musina’s main road has 32 employees producing 70 tons of sausages each month for resale across the border. His family members have also become middlemen for more affluent shoppers, securing boxes of rarities like Pringles, Oreos and, in one case, 130 pounds of chocolate bars to be sent to Zimbabwe’s capital, Harare.

As in much of the world, migrants to South Africa tend to be young, driven and entrepreneurial, adding far more to the economy than just competition for jobs, experts say.

A study by the World Bank found that one immigrant worker typically produces two jobs for South Africans. Another by the Organization for Economic Cooperation and Development found that immigrants contribute 9 percent of South Africa’s gross domestic product.

Migration has indelibly changed Musina, once a sleepy town. South African shop owners rent their storefronts to entrepreneurs from Pakistan, Bangladesh, Ethiopia and Somalia, who moved to Musina to cash in on the bulk-buying trends. A Chinese-owned outlet store is one of the largest businesses in town, selling everything from furniture to building materials.

Zimbabwean buyers usually resell the goods back home — some in shops of their own.

Almost all of Musina’s economy is reliant on cross-border shopping. And there is money to be made at every step of the process, legally and illegally.

By the border bridge and checkpoint, food vendors live and work from shacks erected next to the road. The surrounding area resembles a car dealership, with rows and rows of Japanese-made cars waiting for export to other African countries.

The parking lot of a strip mall has been commandeered by packers, usually men, who charge about $20 to pack and wrap items in a way that can avoid scrutiny at the border.

Maxwell Ntuli, wearing a yellow vest, oversees the scene, guarding against robbers who prey on cross-border customers, who carry large wads of cash.

He worked as a taxi driver for years, but now makes more money at this. In the chaos, Mr. Ntuli, a South African, loudly berates the packers, many of whom are Zimbabweans living in South Africa illegally.

As shoppers scurry back to the border by midday, they are greeted by another set of middlemen in this transnational economy.

To avoid paying high import duties or bribes, shoppers hire porters to carry their wares across the border, often in bulging backpacks. Sometimes, several porters will split the stock among each other and declare it as their personal luggage. Other times, they slip through one of the many gaping holes in the fence, not far from the checkpoint where officials stamp passports. The porters duck behind trees and hide from soldiers camping within view, then dip back into South Africa to pick up more loads.

Two Zimbabwean porters, who identified themselves only as Simba and Justice for fear of arrest, said they supported their families this way. Justice has been a porter for 14 years, while Simba took up the dangerous work in 2018, earning roughly $5 per trip to ferry goods, and nearly $30 to guide people across the Limpopo River. Women, seen as a liability when running from soldiers or crocodiles, are charged more.

“If I’m working hard, I can do four trips in a day,” said Simba, speaking through the razor fence from the Zimbabwean side.

“Me, I’m a lazy boy,” Justice said, laughing. “I only did two trips.”

Getting caught by border security will set them back $50 or three months in jail. Both say they have been caught and deported more times than they can remember.

For heavier loads, other porters say they go downriver where the waters are shallow, and donkeys carry the goods into Zimbabwe. Across the river, a vehicle waiting in the bush delivers the items to their owners in Zimbabwe.

On a recent afternoon in mid-December, Simba and Justice had just crossed the Limpopo and were approaching the fence to enter South Africa when they saw a vehicle approaching. A South African government truck drove by, carrying a new roll of razor wire and workers tasked with fixing the fence. The South Africans and Zimbabweans waved at each other, then carried on with their separate, deeply intertwined journeys.

Jeffrey Moyo contributed reporting from Harare, Zimbabwe.