Such agreements existed at the time and NATO continuously violated them in expanding membership towards Russia. It was a dangerous game and a lot of people have died because of it and because of Putin's paranoia.
So no, whatever the real reason for the SMO was (highly likely to be oil), it was not because Putin/Russia was scared of being attack by NATO, and the agreement to not joining NATO will not stop the invasion.
Edit: I mean, just look at the recent history of Ukraine's friendly intermingling with NATO just prior to the invasion:
https://en.wikipedia.org/wiki/Ukraine%E2%80%93NATO_relations...
You can certainly argue that this was just an excuse for Putin to invade, but there's little evidence to be so confident in that conclusion, and plenty of countervailing evidence.
Sounds like a great argument for all countries that can to acquire nukes.