The Metropolitan Museum of Art doled out hefty pay raises and six-figure bonuses to top executives despite a looming deficit that threatened to reach $40 million, records show.

Although the museum was already losing millions when Daniel Weiss took the helm as president in July 2015, he still got a $300,000 bonus for less than half a year on the job, according to the museum’s 2015 federal tax filings, the latest available for the troubled arts institution.

In fact, Weiss’s total compensation for six months of work came to $818,112, which included a salary of $327,931 and a housing allowance for his Park Avenue apartment.

Outgoing Met director and chief executive Thomas Campbell’s total compensation was $1,428,935, including a salary of $942,287. His salary and “other compensation” of $127,622 was a 7 percent increase over the previous year, records show.

Campbell, who resigned last month amid the growing fiscal crisis, lives across the street from the museum in a grand, four-bedroom, four-bath Fifth Avenue apartment provided by the museum. He has headed the museum since 2009, and is scheduled to leave in June.

Former Met president Emily Rafferty, who left her position at the end of June 2015, received a total compensation package of $2.2 million, the tax records show.

Suzanne Brenner, the Met’s senior VP and chief investment officer and its next-highest-paid employee, saw her bonus rise to $624,828 from $570,590. Her total compensation was $1,583,553.

The salary for chief investment officer Lauren Meserve jumped 14.7 percent and her bonus shot up to $548,723 from $466,847. Her total compensation was $1,451,775.

The Met would not comment on the raises.

Museum executives sounded the alarm about rising deficits during the last fiscal year, which ended June 30, 2016.

They announced austerity measures, including staff cuts and curtailing the number of special exhibitions. The year ended with an $8.3 million deficit.

The Met laid off 34 employees, or 1.5 percent of its workforce of 2,200, last September, although none of the layoffs involved curatorial staff, a spokesman told The Post. Earlier, the museum announced that 50 staff members had taken buyouts.

The museum spent 4 percent of its budget in the last fiscal year operating the Met Breuer, the former Whitney Museum on Madison Avenue it took over to devote to modern art, according to its latest financial report.

The museum also settled a three-year-old class-action lawsuit in 2016 that challenged the Met’s admissions policy, and forced the museum to clarify its “pay what you wish” directive. While there was no large cash payout, the plaintiffs’ lawyer sought $350,000 in legal fees from the Met. A spokesman for the Met said the payment hadn’t been finalized by the court.

Revenue from admissions went up by 4 percent last year and the Met saw record attendance of 7 million, the spokesman said.