(Reuters) - America must work with all nations to build stronger economies, recognizing the inequalities that globalization can generate but refusing to give in to protectionism, U.S. President Barack Obama wrote in the Economist on Thursday.

U.S. President Barack Obama delivers a statement on the Paris Agreement in the Rose Garden of the White House in Washington, U.S., October 5, 2016. REUTERS/Yuri Gripas

Months before he leaves the White House in January, Obama wrote that a certain anxiety over globalization had taken hold in the United States, not unlike the discontent leading to Britain’s vote in June to leave the European Union.

“The world is more prosperous than ever before and yet our societies are marked by uncertainty and unease,” the Democratic president wrote.

“So we have a choice - retreat into old, closed-off economies or press forward, acknowledging the inequality that can come with globalization while committing ourselves to making the global economy work better for all people, not just those at the top.”

Calling capitalism the greatest driver of prosperity the world has ever known, Obama argued that trade had helped the U.S. economy much more than hurt it.

His enthusiastic advocacy for trade runs counter to the stated policies of both his potential successors, who say many trade deals hurt U.S. workers.

The Trans-Pacific Partnership trade deal championed by the president is opposed by Republican presidential nominee Donald Trump and his Democratic rival in the Nov. 8 election, Hillary Clinton, whom Obama has endorsed.

Obama listed four major structural challenges facing the United States - “boosting productivity growth, combating rising inequality, ensuring that everyone who wants a job can get one and building a resilient economy that’s primed for future growth.”

Trumpeting the achievements of his eight-year presidency, led by preventing the 2007-2009 recession from turning into a depression, Obama said a foundation was laid for a better future.

“America must stay committed to working with all nations to build stronger and more prosperous economies for all our citizens for generations to come,” he wrote.