The desert produce is packed and shipped to places like Denmark and Delaware. Electricity and water have come to villages that long had neither. Farmers have moved here from the mountains, seeking new futures on all the irrigated land.

It might sound like a perfect development plan, except for one catch: The reason so much water flows through this desert is that an icecap high up in the mountains is melting away.

And the bonanza may not last much longer.

“If the water disappears, we’d have to go back to how it was before,” said Miguel Beltrán, a 62-year-old farmer who worries what will happen when water levels fall. “The land was empty and people went hungry.”

In this part of Peru, climate change has been a blessing — but it may become a curse. In recent decades, accelerating glacial melt in the Andes has enabled a gold rush downstream, contributing to the irrigation and cultivation of more than 100,000 acres of land since the 1980s.

Yet the boon is temporary. The flow of water is already declining as the glacier vanishes, and scientists estimate that by 2050 much of the icecap will be gone.

Image

A 30-year-old photograph taken from a nearby location was placed on the path to the Pastoruri glacier in Áncash, Peru, showing how far the ice has retreated.CreditTomas Munita for The New York Times

Image

A family harvesting flowers in the foothills of the Cordillera Blanca in Peru.CreditTomas Munita for The New York Times

Image

An irrigation canal running through the desert south of Trujillo, Peru. Accelerating glacial melt in the Andes has allowed more than 100,000 acres of land to be brought into cultivation since the 1980s.CreditTomas Munita for The New York Times

Throughout the 20th century, enormous government development projects, from Australia to Africa, have diverted water to arid land. Much of Southern California was dry scrubland until canals brought water, inciting a storm of land speculation and growth — a time known as the “Water Wars” depicted in the 1974 film “Chinatown.”

Yet climate change now threatens some of these ambitious undertakings, reducing lakes, diminishing aquifers and shrinking glaciers that feed crops. Here in Peru, the government irrigated the desert and turned it into farmland through an $825 million project that, in a few decades, could be under serious threat.

“We’re talking about the disappearance of frozen water towers that have supported vast populations,” said Jeffrey Bury, a professor at the University of California at Santa Cruz who has spent years studying the effects of glacier melt on Peruvian agriculture. “That is the big picture question related to climate change right now.”

A changing climate has long haunted Peru. One past civilization, the Moche people, built cities in the same deserts, only to collapse more than a millennium ago after the Pacific Ocean warmed, killing fish and causing flash floods, many archaeologists contend.

Now dwindling water is the threat. While more than half of Peru sits in the wet Amazon basin, few of its people ever settled there. Most inhabit the dry northern coast, cut off from most rain by the Andes range. While the region includes the capital, Lima, and 60 percent of Peruvians, it holds only 2 percent of the country’s water supply.

The glaciers are the source of water for much of the coast during Peru’s dry season, which extends from May to September. But the icecap of the Cordillera Blanca, long a supply of water for the Chavimochic irrigation project, has shrunk by 40 percent since 1970 and is retreating at an ever-faster rate. It is currently receding by about 30 feet a year, scientists say.

Farmers along the 100-mile watershed that winds its way from the snowcapped peaks to the desert dunes say they are already feeling the effects of the change.

The retreat of the icecap has exposed tracts of heavy metals, like lead and cadmium, that were locked under the glaciers for thousands of years, scientists say. They are now leaking into the ground water supply, turning entire streams red, killing livestock and crops, and making the water undrinkable.

Image

Workers harvesting asparagus in the sand south of Trujillo, Peru.CreditTomas Munita for The New York Times

Temperatures in this area have risen sharply, leading to strange changes in crop cycles, farmers say. Over the past decade, corn — which since precolonial times was grown only once a year in the mountains — can now be harvested in two cycles, sometimes three.

That would be a windfall, said farmers like Francisco Castillo, if it were not for all the pests that now thrive in the warmer air.

For Mr. Castillo, who plants corn and rice near the Santa River in Chimbote, it was a worm that became the scourge for him and neighboring farmers. It suddenly started devouring their crops in the early 2000s.

Then, last year, came the rats.

“This wasn’t a place you had rats before,” Mr. Castillo said.

For Justiniano Daga, a 72-year-old farmer, the breaking point for his cotton crops came when red ants ate away the buds. This year, he has decided to plant sugar cane instead and move some of his production to higher altitudes where it is colder.

The Chavimochic project, which lies just north of where the Santa River meets the Pacific Ocean, is a crown jewel of Peruvian agriculture and civil engineering.

The government aimed to create industrial-scale agriculture in Peru’s northern deserts through a sprawling system of locks and canals. The idea’s supporters promised profits through exports to markets in North America, Asia and Europe, where the fruit seasons were reversed.

The first phase of the project started in 1985 with a 50-mile canal that irrigated a valley and brought a large hydroelectric plant, providing electricity to residents. In the early 1990s, Peru began a second phase, which irrigated two more valleys and created a water treatment plant that served 70 percent of the surrounding population.

Image

The ruins of the Temple of the Sun, built by the Moche people, whose civilization collapsed more than a millennium ago after the Pacific Ocean warmed, killing fish and causing flash floods.CreditTomas Munita for The New York Times

Image

Blueberries in the region grow to five times their normal size. They are shipped to China, where they are prized.CreditTomas Munita for The New York Times

Image

A team of glacier researchers walking in Cordillera Blanca, Peru. The temperature at the site of the glaciers rose between 0.5 and 0.8 degrees Celsius between the 1970s and early 2000s.CreditTomas Munita for The New York Times

All told, more than 100,000 acres of desert were brought into cultivation.

“Years ago, if I gave you a plot of land here, you’d have said, ‘What do I do with this?’ ” said Osvaldo Talavera, a spokesman for the water district. “Now you’d say, ‘Do you have another plot for me?’ ”

Among the investors was Rafael Quevedo, a wealthy Peruvian landowner who began snapping up arid tracts after studying desert hydroponic techniques in Israel. His proposition was simple: With enough water and fertilizer, asparagus could be grown directly in the sand — and at yields per acre far higher than in the United States because Peru has no cold season and more days of sun.

“We’ve started a new chapter in the history of cultivation,” said Mr. Quevedo, who runs a farming company called Talsa.

Blueberries on a sandy hillside here grow to be five times as big as a normal-size blueberry before being sent to China, where they are prized for their size. A form of white asparagus, favored by Europeans, is grown by burying the stalks in sand. A reservoir was created out of a dune. More than 8,000 tons of produce grows here every year.

Yet at the headwaters of the Santa River, in the mountain city of Huaraz, César Portocarrero, a Peruvian climatologist, sees problems afoot for those downstream.

The temperature at the site of the glaciers rose 0.5 to 0.8 degrees Celsius from the 1970s to the early 2000s, causing the glaciers of the Cordillera Blanca to double the pace of their retreat in that period, Mr. Portocarrero said. Several times a year, he and other scientists would make brutal hikes into the glacial valleys, where they found entire sections of the icecap gone. One part of an exposed glacier revealed fossils of dinosaur footprints.

Image

Sand dunes near agricultural lands in Viru Valley, Peru.CreditTomas Munita for The New York Times

A 2012 study by scientists from the United States and Canada showed that water flow in the Santa River was falling, and that at current rates, the river could lose 30 percent of its water during Peru’s dry season.

“Each year there is less water; each day there is less water,” Mr. Portocarrero said.

When Odar Gómez headed to Chavimochic to look for work in 1997, he was only the third person to do so from his impoverished mountain town, where many eked out a living with the subsistence farming of corn.

It was the start of a wave of migration from the mountainside to the coast set off by the arrival of the water.

One coastal town, Viru, went from a population of 9,000 in the 1990s to 80,000 today. Another, Valle de Dios, was an empty desert canyon until it was invaded by squatters in the early 2000s. The new arrivals were mainly agricultural workers and transformed it into a full-fledged town with a pharmacy, a grocery store and a mechanic’s garage.

Mr. Gómez began to work at Talsa, and he headed to the mountains on weekends to recruit others to join him.

“Now in my village you have empty homes where whole families have left,” he said. “People are coming from the Amazon. Chavimochic was our salvation.”

Image

The town of Valle de Dios, which was an empty desert canyon until it was invaded by squatters in the early 2000s.CreditTomas Munita for The New York Times

Image

Support staff members for a research team rested at the base of a glacier after hiking from their base camp.CreditTomas Munita for The New York Times

Image

A shepherd with his flock in the Cordillera Blanca, some 14,000 feet above sea level.CreditTomas Munita for The New York Times

The water has also transformed life on the coast.

About a decade ago, a Danish-Peruvian operation installed running water and electricity in the town of Huancaquito Alto, where the business was employing many residents in its packing plant. The town of 2,500 now has a municipal cleaning system that employs trash collectors.

“This was all grassland,” said Edgar García, a member of the town council, pointing at a new public plaza that was opened last year.

Mercedes Beltrán grew up in San Bartolomé, which was barely a village, with only three families. Her grandfather fished from a traditional reed raft. “There was no market, we bartered between ourselves,” she said.

Now her family plants asparagus for the American market, benefiting from competition between buyers that keeps prices high, she said.

Even the slightest reduction in the flow of the Santa River causes alarm here. The hydroelectric plant now provides power to 50,000 people; treated river water supplies 700,000 people.

“In years to come, we will be fighting over water,” said Mr. Gómez.

The government has struggled to offer solutions. One proposal would try to capture rain runoff from the Andes during the wet season in a large dam. But construction on the dam was led by Odebrecht, a Brazilian construction company that admitted to paying $800 million in bribes throughout Latin America.

The dam is now “completely paralyzed,” with few signs that it will be starting again soon, said Miguel Chávez Castro, the director of the project.

Meanwhile, planners here continue to push for more irrigation. They are now eyeing desert tracts farther to the south for a new 80-mile canal that would, at least for now, supply another 50,000 acres of desert with water.

But Mr. García, the council member in Huancaquito Alto, is not taking any chances. He has refurbished an old well used by his father to hold water in the days before this area was irrigated, and he is building a new one near his asparagus fields.

“Because of this water, our children have been able to go to university,” he said. “But if there is no water from the Santa River, that all changes.”

He added: “We have to get our wells ready. Sure, it’s like going back in time, but what can we do?”

Luis Puell and Andrea Zarate contributed reporting.

A version of this article appears in print on , Section A, Page 1 of the New York edition with the headline: Living Off a Glacier, While It Lasts. Order Reprints | Today’s Paper | Subscribe