A Trump-appointed federal judge has ruled that the US president cannot use the Alien Enemies Act to deport Venezuelan migrants.

US District Judge Fernando Rodriguez said Donald Trump’s use of the wartime power was “unlawful” and had been improperly invoked.

The law was written in 1798 to allow the removal of non-citizens in times of war or invasion. It has been used by Trump to deport Venezuelans to El Salvador on the basis they were members of the Tren de Aragua gang and “conducting irregular warfare”.

The judge said the act only applied when the US was facing an “armed organised attack”, which it is not. The administration has not commented.