Because China was actually pretty far from India.
For most of the past millennia, China and India were not "neighbouring countries" in any meaningful sense of the word. Most Chinese empires did not actually stretch all the way to the Indian subcontinent. It seems you're considering China and India based on their modern borders, but that is misleading: modern China possesses vast territories beyond its historic core.
Although the Chinese established wide ranging empires at different points of history, they did so from a home country that's roughly located like the red shaded region below:
The maximum extent of Chinese empires (the yellow bits) reflected the limits of their logistics from home. This "supply range", if you will, reached its longest under the sophisticated military science of the Manchurian war machine, during the Qing Dynasty.
The hostile geography in India's direction, and huge distances involved, made it difficult for China to actively pursue war any further from home.
For reference, expeditions to Korea were major causes in ruining the once mighty native Chinese Sui and Ming Empires. And Korea could be resupplied by ships.
Actually, China has not historically been particularly adept at waging large scale war over long distances. Most of China's modern conquests are a legacy of its Manchurians conquerors, who ruled China for most of the modern era up until 1911.
Now, the maximum extent of Qing rule is mostly preserved today barring Manchuria and Mongolia. If you look at the modern border,
You can see that India actually neighbours Tibet and, further north, Xinjiang. Although both regions are now under the ultimate control of Beijing, that is not the case for most of recorded history.
Literally "New Territories", Xinjiang was home to a series of different peoples and polities until 1759. This regions were at times under a strong China's influence, notably during the Tang Empire, but invariably Chinese power would wane amid civil war and internal unrest. None of China's previous "conquests" here, which were often achieved via diplomatic means (notably by wedding princesses to local rulers), lasted long.
That changed only in the mid-18th century, when the Qing Empire launched a series of brutal wars that forcibly put the region into the Qing Empire in a process so bloody it has been termed a genocide.
Roughly translated: They have no appeals to Heaven and no place on Earth. They brought this on themselves. They will leave none of their seed in their homeland. Of the hundreds of thousands of families, four in ten died of disease; two in ten fled into Russia and Kazakhstan; three in ten were vanquished by our army. With the exception of women and children taken as rewards, only a handful of Oirat(?) families who surrendered remained. Other than that there not a single tent remained across thousands of miles.
-- A Military History of the Qing Dynasty, by Wei Yuan.
Once upon a time Tibet was, in fact, a powerful empire that posed a credible threat to Tang China. That did not last; however, like Korea, Tibet remained a distinct polity separate from China in most of the centuries since. While Tibet was always liable to be within the Chinese sphere of influence (again c.f. Korea), this only became more pronounced from the late 18th century onward.
It is difficult to say when exactly did Tibet fell to China. A good point however is 1792, when the Qing Empire, fresh from saving Tibet from the Nepalese invasion, was able to impose unprecedented controls on Tibet. The most important measure taken was the Chinese intervention in the succession of Tibet's religious/political leadership.
Nonetheless, as late as 1904 Great Britain could still sign a treaty with Tibet: the Convention Between Great Britain and Thibet (1904). And in 1907, two European Great Powers would declare:
In conformity with the admitted principle of the suzerainty of China over Thibet, Great Britain and Russia engage not to enter into negotiations with Thibet except through the intermediary of the Chinese Government.
-- Article II, Convention Between Great Britain and Russia (1907)
This status was explicitly confirmed by China as well in 1914's Simla Accord:
The Governments of Great Britain and China recognizing that Tibet is under the suzerainty of China, and recognizing also the autonomy of Outer Tibet, engage to respect the territorial integrity of the country, and to abstain from interference in the administration of Outer Tibet (including the selection and installation of the Dalai Lama), which shall remain in the hands of the Tibetan Government at Lhasa.
The Government of China engages not to convert Tibet into a Chinese province. The Government of Great Britain engages not to annex Tibet or any portion of it.
-- Article II, Convention Between Great Britain, China, and Tibet (1914)
Tibet subsequently expelled Chinese troops and became de facto fully independent until 1951, when Communist forces annexed Tibet to the newly created People's Republic of China.
China and India had peace for a long time because:
- Until relatively recently, Tibet and modern Xinjiang were massive buffer state/region/Turkic tribes.
- The geography made war an unattractive prospect
- China proper, which was not usually particularly adept at long distance warfare, is very very far from India
Note also that they weren't really at peace all that time either. Even when separated by large distances and the Himalayas, Chinese and Indian polities still actually did fight each other on at least two occasions:
I would also include these, but I acknolwedge they are arguable: