The government of Macedonia secured just enough support from Parliament late Friday to keep alive a deal to change the country’s name, a move that could end a bitter, decades-old dispute with Greece and set the Balkan nation on a path to joining NATO.

Parliament voted by the slimmest of margins to alter the Constitution to rename the country as the Republic of North Macedonia, giving a tenuous victory to the government of Prime Minister Zoran Zaev and his Western allies.

Eighty members of Parliament voted in favor of the measure — barely meeting the two-thirds majority needed for constitutional changes — after a week of contentious talks that culminated in 12 hours of intense negotiations on Friday.

The move by Parliament came even though a referendum last month failed to attract sufficient turnout to endorse the name change.

“Our journey toward a better future, toward E.U. and NATO membership has just begun,” Mr. Zaev said after the vote, though he acknowledged much work awaited.

Indeed, the path ahead remains complicated and the end result far from certain.

Lawmakers now have 30 days to prepare amendments to the Constitution, and those must pass before the agreement can be taken up in the Greek Parliament, where significant opposition would have to be overcome. Just this week, the Greek foreign minister resigned as members of Prime Minister Alexis Tsipras’s fragile governing coalition fought over the deal.

The dispute about Macedonia’s name stretches back to 1991, when Macedonia declared independence as the former republic of Yugoslavia broke apart.

Greece refused to recognize the newly formed nation, viewing its decision to call itself Macedonia as a thinly veiled claim on territory in northern Greece, including the strategically important port of Thessaloniki.

Despite Macedonia’s assurances over years of negotiations that it would make no claims on Greek lands, the issue proved intractable.

For many Greeks — who refer to the Balkan country as Skopje, the name of the capital — any use of the name Macedonia by their neighbors to the north is an attempt to distort history and lay a false claim to Greek culture.

According to that argument, ancient Macedonia was a Hellenistic society and the name is for the Greeks alone to claim.

At the same time, many Macedonians, whose country’s internationally recognized name is the Former Yugoslav Republic of Macedonia, say that no country has the right to tell another nation what it can call itself.

The previous nationalist government in Macedonia tapped into the passions surrounding the issue. It stoked Greek anger by renaming Skopje’s airport after Alexander the Great and erecting scores of statues to the ancient Macedonian warrior.

So when Mr. Zaev joined his Greek counterpart, Mr. Tsipras, on the shores of Lake Prespa in northern Greece in June to announce an agreement between the two nations, it was widely hailed in the West as a milestone.

“This is our own rendezvous with history,” Mr. Tsipras said at the time.

But it was a decision greeted by protests in the two countries, and it could still prove the undoing of both governments.

The deal has fueled political upheaval in Greece, with the country’s foreign minister, Nikos Kotzias, resigning on Wednesday after reports of a growing rift in the ruling coalition over the contentious agreement.

Mr. Kotzias, who signed the name deal in June, is believed to have been at odds with the defense minister, Panos Kammenos, Mr. Tsipras’s junior coalition partner who has vociferously opposed the agreement and proposed an alternative plan during an official visit to Washington.

Mr. Tsipras told reporters that he would assume the Foreign Ministry post in a bid to underline his commitment to the name deal. He said he would no longer tolerate any “duplicity” or “personal agenda” from his ministers that could put the country’s recovery at risk so soon after its exit from international bailouts.

In Macedonia, the main opposition party, the nationalist VMRO-DPMNE, called for lawmakers to vote against proceeding with the deal.

Instead, it said its members should prevent the government from negotiating any change of the Constitution. The party called for the establishment of “strategic partnerships” with the European Union and with NATO, without becoming members of these alliances, until the name issue was resolved.

Western leaders made it clear in recent weeks, however, that if Macedonia failed to support the deal now, another chance to join NATO might not come for a long time.

Leaders from the United States and Europe applauded the agreement as a chance to bring stability to the historically volatile region.

At the same time, Moscow, long opposed to NATO expansion, worked to undermine the deal, according to U.S. intelligence officials and authorities in both Greece and Macedonia.

As the Macedonian Parliament debated the issue, the Russian Foreign Ministry issued a statement condemning what it said was Western pressure on those opposed to the deal.

“A barrage of appeals from overseas literally came down on opposition deputies,” the ministry wrote. “There is an undisguised continuation of the crudest interference of the U.S. and the E.U. in the internal affairs of Skopje.”

While Moscow said that the matter should be decided by Macedonia and Greece without outside interference, Western officials have accused Russia of using a variety of means to undermine the deal.

American intelligence agencies moved aggressively to push back on what they say was a Russian influence campaign in Greece, going as far as turning over to the Greek government intercepted communications that showed how a Greek-Russian billionaire, Ivan Savvidis, had worked as a conduit for Moscow to sabotage the agreement.

In last month’s referendum, more than 90 percent of those who voted supported the agreement, but less than 36 percent of registered voters went to the polls. The low turnout meant the issue had to be taken up in Parliament.

In Macedonia, Western officials expressed concern before the referendum that not enough had been done to counter the torrent of false reports flooding social media, much of it directed by Russia.

A campaign urging people to boycott the referendum was successful for several reasons — not least because changing the nation’s name has long been deeply unpopular in Macedonia.

Many who supported the name change did so not because they loved the agreement, but because it came with assurances that the country would then be admitted into NATO and set on a path to join the European Union. It was a message drilled home by a parade of Western leaders who visited the country in the days before the vote.

Angela Merkel became the first German chancellor to visit the country, and NATO’s secretary general, Jens Stoltenberg, called the decision a “once-in-a-lifetime opportunity.”

By MARC SANTORA and ALEKSANDAR DIMISHKOVSKI