The Senate voted a major change in U.S. nuclear nonproliferation policy yesterday by requiring the president to suspend all foreign aid to any currently nonnuclear country that explodes a nuclear device.

The action, which surprised the administration, could eventually affect such U.S. friends as Israel, South Africa and Taiwan, all of which are suspected of seeking to develop nuclear weapons and which also benefit from American aid programs.

The unexpected amendment was offered by Sen. Jesse Helms (R-N.C.) to a bill authorizing $5.7 billion in foreign aid this fiscal year.

The Senate in a defeat for the administration had just voted 51 to 45 to require suspension of foreign aid to Pakistan or India if either country detonates a nuclear device. Aimed primarily at Pakistan, which is believed to be developing nuclear weapons, that amendment was offered by Sen. John Glenn (D-Ohio).

Helms then proposed that the Glenn amendment be broadened to apply to all new entrants into the nuclear club. The conservative Carolinian, normally an administration supporter, this time chided the administration for failing to live up to its professed nonproliferation policy. "I'm surprised the administration is not leading the effort to put some muscle into its nonproliferation policy," he said.

But once Glenn prevailed, the administration welcomed Helms' amendment, which passed on a voice vote. "We don't want to be in the position of discriminating against Pakistan," a State Department official said, and added that the Helms variation "will be easier to explain to Pakistan."

Republican leaders fought the Glenn amendment on grounds it would tie a president's hands just at the time the administration is seeking to make Pakistan an anti-Soviet bulwark.

Glenn defended it as necessary to discourage Pakistan's ambitious nuclear weapons programs and to assure other developing countries of U.S. seriousness about discouraging proliferation.

He charged that Pakistan's purchase of nuclear equipment in several countries in recent years is proof that country is bent on producing nuclear weapons despite its repeated denials. To resume unrestricted aid to Pakistan would be a signal to other countries that nonproliferation policy is meaningless, Glenn said.

American aid to Pakistan was banned two years ago because of its suspected weapons program. But the Reagan administration sought this year to revive both economic and military aid because of the Soviet invasion of neighboring Afghanistan. It has promised a $3.2 billion aid program that includes fighter planes and other arms. In addition, the new foreign aid bill proposes another $100 million in purely economic support.

The administration won a victory when the Foreign Relations Committee agreed in its version of the bill to let Reagan resume aid to Pakistan. Opponents said they lacked the votes to knock out that provision on the floor, and were forced instead just to qualify it.

Pakistan has formally denied that it is bent on producing weapons but has refused to give assurances that it will not detonate a nuclear device. It is currently building a uranium enrichment plant. Under Glenn's amendment, the president must immediately suspend all aid to Pakistan if it explodes a nuclear device. India, which exploded one in the early 1970s, also is subject to the cutoff.

The Senate quit last night without completing action on foreign aid. A comparable foreign aid bill is awaiting floor action in the House. It contains no change in current law on aid to Pakistan.

The Senate yesterday also approved a sense of the Congress resolution condemning Libya for sponsoring international terrorism and calling on the administration to study ways to pressure that country into stopping terrorist tactics.

The resolution, introduced by Foreign Relations Committee Chairman Charles H. Percy (R-Ill.), was a substitute for an amendment that would have required a cutoff within 90 days of all American oil imports from Libya as a means of forcing that country to cease terrorism.

Both Maryland senators voted for the Glenn amendment, as did Harry F. Byrd (Ind.-Va.). Sen. John W. Warner (R-Va.) voted against it.