Planets form in the discs of gas and dust that surround young stars. It is not known whether gas giant planets on wide orbits form the same way as Jupiter or by fragmentation of gravitationally unstable discs. Here we show that a giant planet, which has formed in the outer regions of a protostellar disc, initially migrates fast towards the central star (migration timescale ~10,000 yr) while accreting gas from the disc. However, in contrast with previous studies, we find that the planet eventually opens up a gap in the disc and the migration is essentially halted. At the same time, accretion-powered radiative feedback from the planet, significantly limits its mass growth, keeping it within the planetary mass regime (i.e. below the deuterium burning limit) at least for the initial stages of disc evolution. Giant planets may therefore be able to survive on wide orbits despite their initial fast inward migration, shaping the environment in which terrestrial planets that may harbour life form.