The first leg of the 35-mile journey from Ghana’s capital city, Accra, to the Fairafric chocolate factory in Amanase on the N6 highway is a quick ride. But after about 30 minutes, the smoothly paved road devolves into a dirt expanse without lanes. Lumbering trucks, packed commuter minivans, cars and motorcycles crawl along craggy, rutted stretches bordered by concrete dividers, muddy patches and heaps of rock.

The stopgap roadway infrastructure is one of the challenges Fairafric has had to navigate to build a factory in this West African country. The area had no fiber-optic connection to Ghana’s telecommunications network. No local banks were interested in lending the company money. And it required the personal intervention of Ghana’s president before construction could even begin in 2020.

The global chocolate industry is a multibillion-dollar confection, and Africa grows 70 percent of the world’s raw cocoa beans. But it produces only 1 percent of the chocolate — missing out on a part of the business that generates the biggest returns and is dominated by American and European multinationals.

Capturing a bigger share of the profits generated by chocolate sales and keeping them in Ghana — the second-largest cocoa exporter behind Ivory Coast — is the animating vision behind Fairafric. The aim is to manufacture the chocolate and create stable, well-paying jobs in the place where farmers grow the cocoa.

Many developing countries are lucky to have large reserves of natural resources. In Ghana, it’s cocoa. In Botswana, it’s diamonds. In Nigeria and Azerbaijan, it’s oil. But the commodity blessing can become a curse when the sector sucks up an outsize share of labor and capital, which in turn hampers the economy from diversifying and stunts long-term growth.

“Look at the structure of the economy,” Aurelien Kruse, the lead country economist in the Accra office of the World Bank, said of Ghana. “It’s not an economy that has diversified fully.”

The dependency on commodities can lead to boom-and-bust cycles because their prices swing with changes in supply and demand. And without other sectors to rely on during a downturn — like manufacturing or tech services — these economies can crash.

“Prices are very volatile,” said Joseph E. Stiglitz, a former chief economist at the World Bank. In developing nations dependent on commodities, economic instability is built into the system.

But creating industrial capacity is exceedingly difficult in a place like Ghana. Outside large cities, reliable electricity, water and sanitation systems may need to be set up. The suppliers, skilled workers, and necessary technology and equipment may not be readily available. And start-ups may not initially produce enough volume for export to pay for expensive shipping costs.

Fairafric might not have succeeded if its founder and chief executive — a German social-minded entrepreneur named Hendrik Reimers — had not upended the status quo.

The pattern of exporting cheap raw materials to richer countries that use them to manufacture valuable finished goods is a hangover from colonial days. Growing and harvesting cocoa is the lowest-paid link in the chocolate value chain. The result is that farmers receive a mere 5 or 6 percent of what a chocolate bar sells for in Paris, Chicago or Tokyo.

Mr. Reimers’s goal is aligned with the “fairchain movement,” which argues that the entire production process should be in the country that produces the raw materials.

The idea is to create a profitable company and distribute the gains more equitably — among farmers, factory workers and small investors in Ghana. By keeping manufacturing at home, Fairafric supports other local businesses, like the paper company that supplies the chocolate wrappers. It also helps to build infrastructure. Now that Fairafric has installed the fiber optic connections in this rural area, other start-up businesses can plug in.

The last few years have severely tested the strategy. Ghana’s economy was punched by the coronavirus pandemic. Russia’s invasion of Ukraine fueled a rapid increase in food, energy and fertilizer prices. Rising inflation prompted the Federal Reserve and other central banks to raise interest rates.

In Ghana, the global headwinds exacerbated problems that stemmed from years of excessive government spending and borrowing.

As inflation climbed, reaching a peak of 54 percent, Ghana’s central bank raised interest rates. They are now at 30 percent. Meanwhile, the value of the currency, the cedi, tumbled against the dollar, more than halving the purchasing power of consumers and businesses.

At the end of last year, Ghana defaulted on its foreign loans and turned to the International Monetary Fund for emergency relief.

“The economic situation of the country has not made it easy,” said Frederick Affum, Fairafric’s accounting manager. “Every kind of funding that we have had has been outside the country.”

Even before the national default, Ghana’s local banks were drawn to the high interest rates the government was offering to attract investors wary of its outsize debt. As a result, the banks were reluctant to invest in local businesses. They “didn’t take the risk of investing in the real economy,” said Mavis Owusu-Gyamfi, the executive vice president of the African Center for Economic Transformation in Accra.

Fairafric started with a crowdsourced fund-raising campaign in 2015. A family-owned chocolate company in Germany bought a stake in 2019 and turned Fairafric into a subsidiary.

In 2020, a low-interest loan of 2 million euros from a German development bank that supports investments in Africa by European companies was crucial to getting the venture off the ground.

Then the pandemic hit, and President Nana Akufo-Addo closed Ghana’s borders and suspended international commercial flights. The shutdown meant that a team of German and Swiss engineers who had been overseeing construction of a solar-powered Fairafric factory in Amanase could not enter the country.

So Michael Marmon-Halm, Fairafric’s managing director, wrote a letter to the president appealing for help.

“He opened the airport,” Mr. Marmon-Halm said. “This company received the most critical assistance at the most critical moment.”

Both Ghana and Ivory Coast, which account for 60 percent of the world cocoa market, have moved to raise the minimum price of cocoa and expand processing inside their borders.

In Ghana, the government created a free zone that gives factories a tax break if they export most of their product. And this month, Mr. Akufo-Addo announced an increase in the minimum price that buyers must pay farmers next season.

Fairafric, which buys beans from roughly 70 small farmers in the eastern region of Ghana, goes further, paying a premium for its organically grown beans — an additional $600 per ton above the global market price.

Farmers harvest the ripe yellow pods by hand, and then crack them open with a cutlass, or thick stick. The pulpy white beans are stacked under plantain leaves to ferment for a week before they are dried in the sun.

On the edge of a cocoa farm in Budu, a few minutes from the factory, a bare-bones, open-sided concrete shed with wooden benches and rectangular blackboards houses the school. Attendance is down, the principal said, because the school has not been included in the government’s free school feeding program.

The factory employs 95 people. They have health insurance and are paid above the minimum wage. Salaries are pegged to the dollar to protect against currency fluctuations. Because of spotty transportation networks, the company set up a free commuter van for workers. Fairafric also installed a free canteen so all the factory shifts can eat breakfast, lunch or dinner on site.

Mr. Marmon-Halm said the company was looking to raise an additional $1 million to expand. He noted that the chocolate industry generated an enormous amount of wealth.

But “if you want to get the full benefit,” he said, “you have to go beyond just selling beans.”