Anas bin Malik reported,

The site of the Prophet's ﷺ mosque belonged to Banu Najjar. It contained palm trees and graves of polytheists. The Prophet ﷺ said to them, "Set me a price for it."They replied, "We will never accept any price for it."The Prophet ﷺ then began building it while they handed him the materials, and the Prophet ﷺ was saying, "Indeed, true life is the life of the Hereafter, so forgive the Ansar and the Muhajirun."He said, before the mosque was built, the Prophet ﷺ used to pray wherever the time of prayer overtook him.

Ibn Majah 742

Sahih

حدثنا علي بن محمد حدثنا وكيع عن حماد بن سلمة عن أبي التياح الضبعي

عن أنس بن مالك قال

كان موضع مسجد النبي ﷺ لبني النجار وكان فيه نخل ومقابر للمشركين فقال لهم النبي ﷺ ثامنوني به قالوا لا نأخذ له ثمنا أبدا قال فكان النبي ﷺ يبنيه وهم يناولونه والنبي ﷺ يقول ألا إن العيش عيش الآخره فاغفر للأنصار والمهاجره قال وكان النبي ﷺ يصلي قبل أن يبني المسجد حيث أدركته الصلاة

ابن ماجه 742صحيح
Contents
Reference
Commentary
Ayat
Collection
Sunan Ibn Majah
Book
Mosques and congregations
Chapter
Where it is permissible to build mosques
Numbering
Darussalam 742
Ibn Majah 742