WASHINGTON — The death of Justice Ruth Bader Ginsburg on Friday instantly upended the nation’s politics in the middle of an already bitter campaign, giving President Trump an opportunity to try to install a third member of the Supreme Court with just weeks before an election that polls show he is currently losing.

The White House had already made quiet preparations in the days before Justice Ginsburg’s death to advance a nominee without waiting for voters to decide whether to give Mr. Trump another four years. Senator Mitch McConnell, Republican of Kentucky and the majority leader, vowed Friday night to hold a vote on a Trump nominee but would not say whether he would try to rush it through before the vote on Nov. 3 in what would surely be a titanic partisan battle.

The sudden vacancy on the court abruptly transformed the presidential campaign and underscored the stakes of the contest between Mr. Trump and former Vice President Joseph R. Biden Jr., his Democratic challenger. It also bolstered Mr. Trump’s effort to shift the subject away from his handling of the coronavirus pandemic and remind Republicans why it matters whether he wins or not, while also potentially galvanizing Democrats who fear a change in the balance of power on the Supreme Court.

If Mr. Trump were able to replace Justice Ginsburg, a liberal icon, it could cement a conservative majority for years, giving Republican appointees six of the nine seats. While Chief Justice John G. Roberts Jr. lately has sided at times with the four liberals on issues like immigration, gay rights and health care, he would no longer necessarily be the swing vote on a court with another Trump appointee.

The justice’s death presents a major challenge to Mr. McConnell, who blocked President Barack Obama from filling a vacancy on the bench in 2016 on the grounds that it should wait until after voters decided on a new president. That move rallied conservative support for Mr. Trump in the election and, after his victory, allowed him to put Justice Neil M. Gorsuch on the court.

Despite that precedent, Mr. McConnell has said that in case of an opening this year, he would try to push through a Trump nomination before the election, arguing that it was a different situation because this time the president and Senate majority are from the same party. But with just a 53-vote majority, it was not immediately clear whether he could hold his party behind such a move.

“Americans re-elected our majority in 2016 and expanded it in 2018 because we pledged to work with President Trump and support his agenda, particularly his outstanding appointments to the federal judiciary,” Mr. McConnell said in a statement on Friday night. “Once again, we will keep our promise. President Trump’s nominee will receive a vote on the floor of the United States Senate.”

Mr. McConnell was notably unclear, however, about the timing, not saying explicitly whether he would hold such a vote before the election or wait until a lame-duck session afterward. A Trump administration official said there may not be enough time on the calendar to vote on a confirmation before the election.

Mr. McConnell sought to stave off defections in a letter later in the evening to fellow Republican senators, imploring them not to take a position until meeting with him in Washington. “I urge you all to keep your powder dry,” he wrote. “This is not the time to prematurely lock yourselves into a position you may later regret.”

Several members of Mr. McConnell’s caucus face tough election contests and might balk at seeming to rush a nominee through in such highly partisan conditions. Mr. McConnell too faces a vigorous challenge in his home state.

ImageSenator Mitch McConnell, the majority leader, said in a statement on Friday night that “President Trump’s nominee will receive a vote on the floor of the United States Senate.”
Credit…Doug Mills/The New York Times

Senator Susan Collins of Maine, the most endangered Republican incumbent, told The New York Times this month that she would not favor voting on a new justice in October.

“I think that’s too close,” she said. “I really do.”

Senator Lisa Murkowski of Alaska told an interviewer on Friday shortly before the announcement of Justice Ginsburg’s death that she opposed confirming a new justice before Nov. 3. “I would not vote to confirm a Supreme Court nominee,” she said. “We are 50 some days away from an election.”

Senator Lindsey Graham of South Carolina, the chairman of the Senate Judiciary Committee, which would consider any nominee, said in 2018 that if an opening occurred in the last year of Mr. Trump’s term, “we’ll wait to the next election,” but then hedged in July, telling CNN that “we’d have to see.” In a statement issued Friday night mourning Justice Ginsburg, Mr. Graham, who is in a competitive race of his own, made no mention of when to fill the vacancy.

Senator Charles E. Grassley of Iowa, who was previously the judiciary panel’s chairman, likewise said in 2018 that if there were a vacancy in 2020, he would not bring a nomination before his committee until after the election if he were still in charge. But he has surrendered his gavel to Mr. Graham, and he did not say on Friday night how he would vote as an individual senator. Nor did Senator Mitt Romney of Utah, considered another possible defector.

Democrats immediately said they would fiercely resist any effort to confirm a justice before Inauguration Day, warning that Republicans should follow their own precedent from 2016.

“They must exhibit a shred of integrity and recognize that abandoning their word now, and breaking all precedents by ramming a nominee through — most likely after the election — would cause the nation tremendous pain,” said Senator Patrick J. Leahy, Democrat of Vermont.

Senator Chuck Schumer of New York, the Democratic leader, posted a Twitter message repeating Mr. McConnell’s own words from 2016: “The American people should have a voice in the selection of their next Supreme Court Justice. Therefore, this vacancy should not be filled until we have a new president.”

No one understood the broader political consequences of her death better than Justice Ginsburg, who battled through one ailment after another in hopes of hanging onto her seat until after the election. Just days before her death, NPR reported, she dictated this statement to her granddaughter, Clara Spera: “My most fervent wish is that I will not be replaced until a new president is installed.”

On the campaign trail in recent days, Mr. Trump has stressed the possibility that he could name more members to the court, rolling out a list of about 40 possible candidates he said he would consider. He was onstage on Friday night at a rally in Minnesota when news of Justice Ginsburg’s death arrived and, not aware of what had happened, told the crowd that he planned to name a conservative if given the opportunity.

In describing his potential nominees, he specifically named Senator Ted Cruz, Republican of Texas, joking that he would be unanimously confirmed because Mr. Cruz is so widely disliked by his colleagues that they would love to see him go.

“I have to have somebody that we’re going to make sure we get approved,” the president said. “The only one I can think of is Ted because he’s going to get 50 Republican votes and he’s going to get 50 Democrat votes — they’ll do anything to get him out of the Senate.”

Later in the rally, Mr. Trump returned to the theme, noting that the next president could appoint as many as four justices. In the crowd, someone shouted, “Ginsburg is dead,” but it did not appear that the president heard.

After the rally, aides spoke in his ear as he walked to Air Force One, but when reporters asked about Justice Ginsburg’s passing, he indicated surprise. “She just died?” he said. “Wow. I didn’t know that. You’re telling me now for the first time. She led an amazing life. What else can you say? She was an amazing woman, whether you agreed or not. She was an amazing woman who led an amazing life. I’m actually sorry to hear that.” He made no comment on a replacement and took no questions.

Mr. Biden learned on a flight home from a campaign stop in Minnesota and likewise praised Justice Ginsburg before addressing the looming fight over her replacement.

“The voters should pick the president and the president should pick the justice for the Senate to consider,” he said. “This was the position the Republican Senate took in 2016 when there were almost 10 months to go before the election. That’s the position that the United States Senate must take today, and the election’s only 46 days off.”

Mr. Obama weighed in, too, issuing a written statement saying that four years ago Republicans “invented the principle that the Senate shouldn’t fill an open seat on the Supreme Court before a new president was sworn in” and that basic fairness dictated that “we apply rules with consistency, and not based on what’s convenient or advantageous in the moment.”

Mr. Cruz has already said he would not be interested in a seat on the court, and administration officials indicated they had other choices in mind. The broader list, they said, has been narrowed down to a much shorter one that includes at least one woman. Judge Amy Coney Barrett of the United States Court of Appeals for the Seventh Circuit in Chicago, a favorite of conservatives, has often been mentioned by Mr. Trump’s advisers in the past.

Others on the list who have been on Mr. Trump’s previous rosters of candidates include Judges Thomas M. Hardiman of the Third Circuit, in Philadelphia, and William H. Pryor Jr. of the 11th Circuit, in Atlanta. The president also cited two other Republican senators, Tom Cotton of Arkansas and Josh Hawley of Missouri, as well as his former solicitor general, Noel J. Francisco.

White House advisers privately described Justice Ginsburg’s death as a significant boost for Mr. Trump’s re-election chances. One person familiar with White House planning said that the new nominee would be announced sooner rather than later, and that the White House hoped that Mr. McConnell would move forward with a vote. The president is likely to meet again with those on his short list in the coming days, the person familiar with the planning said.

Democrats argued that the open seat would rally their own supporters as well, but it was not clear it would make a major difference because they are already motivated to defeat Mr. Trump for other reasons.

“I don’t know how much angrier the left can get,” said Jennifer Palmieri, the communications director for Hillary Clinton’s 2016 campaign. She said that Democrats would be animated by the turn of events, and that if Mr. McConnell were to go ahead before the election to move the nomination through, it would cost Republicans the Senate.

Hundreds of admirers of Justice Ginsburg made their way to the Supreme Court on Friday night, holding candles, singing “Amazing Grace” and chanting her initials: “R.B.G., R.B.G., R.B.G.” Flowers accumulated on the court steps, while dozens of people sat silently nearby.

“She stood up for me, and she stood up for you,” said Molly Gilligan of Arlington, Va. “This is a devastating moment, but she gave us a lot to celebrate, as well.”

On the steps closer to the street, people laid signs with some of Justice Ginsburg’s most memorable quotes. But the conversation was never far away from what comes next. “Honor her wish,” the crowd chanted at one point.

“I think it’s tragic that the first thought that a lot of people had was to think about what is the impact on the upcoming elections,” said Massiel Sepulveda of Washington, who joined the mourners at the court. “We can’t even properly mourn a woman who, in her own right, was inspiring for many, who broke down barriers, who set important precedents for women all across this country and across the world.”

Peter Baker reported from Washington, and Maggie Haberman from New York. Adam Liptak and Zach Montague contributed reporting from Washington, and Michael Crowley from Bemidji, Minn.