This is a great question and one that definitely requires some consideration. I have done long commutes, longer than 40 minutes, and not only did it wear on me and my family life, but I began to resent my house because it tied me to a place so far from work.
On the other hand, I've lived places that I didn't love, and was never truly satisfied with where I lived because the place I really felt drawn to was not where I chose to buy. So you have pros and cons on both sides of this.
My best advice to you would be to consider what is going to cause you the greatest stress as well as the greatest joy. Is a 40-minute commute each way (nearly an hour and a half on the road 5 days a week, equaling more than six hours of commute time being added to each work week and taking that time away from loved ones) going to be offset and counter-balanced by living in a place you LOVE, rather than a place you LIKE? You don't want to buy something you won't be happy with, but you also don't want to sacrifice your quality of life for a house. I wish you the best and hope that all of this consideration brings you clarity about what to do!