The U.S. merchandise trade deficit widened to $12.7 billion in May, the second-largest monthly deficit ever, prompting Commerce Secretary Malcolm Baldrige to predict a record deficit of between $140 billion and $150 billion for the year.

The trade deficit last month surpassed the previous record of $13.8 billion in July 1984 and the deficit in April of $11.9 billion, the Commerce Department said.

Imports increased last month to their highest level since July 1984 and exports were the lowest since February of last year, the Commerce Department said.

The trade deficit with Japan increased from $4 billion to $4.2 billion, a figure noted by Baldrige.

However, the value of the shipments of Japanese cars declined from $1.4 billion to $1.2 billion.

The trade deficit with Japan has been the source of much protectionist sentiment in Congress and harsh words from Reagan administration trade negotiators.

The high value of the dollar also has been blamed for the worsening trade problems because a rising dollar makes exports relatively more expensive for overseas customers and imports cheaper for Americans.

Baldrige said that the dollar has declined about 8 percent since February, but it still is at the level of last October and is 43 percent higher than the average for 1980.

"We need further declines in the value of the dollar and faster economic growth abroad to halt the deterioration in the trade deficit," Baldrige said. "We will have another record deficit this year of $140 billion to $150 billion."

For the first six months this year the trade deficit was $57.3 billion, or $138 billion at an annual rate, Commerce said.

"The trade deficit continues to be an economic disaster because it is eroding our industrial base, and it looks as if this will continue unless the dollar declines," said Jerry Jasinowski, chief economist for the National Association of Manufacturers.

"Despite the recent decline in the dollar, the exchange rate is still well above its average level of 1984, when it was already overvalued by as much as 50 percent. Unless the dollar continues to fall, no major improvement in the trade situation can be expected."

"We've expected the trade deficit to continue to deteriorate, although there have been declines in the value of the dollar," said Ed Warren, economist with Chase Econometrics. American manufacturers "are still facing strong competition from imports," he said.

Agricultural exports, including soybeans, tobacco and corn, continued to decline last month. Other declines were in exports of automobiles and power-generating machinery. Overseas sales of aircraft, electric machinery, organic chemicals and bituminous coal increased.

The value of petroleum product imports dropped 1.1 percent in May, although imports increased in organic chemicals, clothing and aircraft. Imports declined for tubes, transistors and semiconductors; telecommunications equipment, and nonmonetary gold.

The trade deficit with Western Europe rose from $1.7 billion to $2.8 billion last month, but the deficit with Canada declined from $2.1 billion to $1.6 billion.