I think this kind of writing is intended to inspire a "holy fear" of God, against those who might sin with complacency
Also to inspire a kind of love of virtue, and a striving towards doing good:
> "If you want to be certain of being in the number of the Elect, strive to be one of the few, not one of the many. And if you would be quite sure of your salvation, strive to be among the fewest of the few" -St. Anselm
https://www.olrl.org/snt_docs/fewness.shtml
I believe the point of Matthew 5:22 is not to teach you how to be saved, but rather to make the point that in order to theoretically be worthy of heaven, you must be perfect. It's supposed to break you and make you realize that your righteousness will never be good enough, and that you need the perfect righteousness of Jesus Christ to save you. You need to realize that your works will always come short.
Matthew 19:16-30 is making the same point also. After describing various ways of how to be perfect (Jesus literally even says in verse 21 "if thou wilt be perfect" and continues on) we come to verse 25 and 26:
> When his disciples heard it, they were exceedingly amazed, saying, Who then can be saved? But Jesus beheld them, and said unto them, With men this is impossible; but with God all things are possible.
So after describing in detail how to live a perfect life, the disciples are astonished and wonder who could even do such a thing, and Christ answers that only with God can you be perfect (i.e. your works cannot save you, but putting your faith in the works of Christ and being covered in His righteousness will).
James 2 is not about salvation and entering heaven. If you read the full context, it's talking about the usefulness of your faith in this life, and is supposed to motivate you to not be a lazy Christian. This makes sense from a free grace perspective, because the natural first reaction to hearing free grace is "so I can just live however I want?" and, in theory, yes, you can (hence Romans 4:4-5), but that doesn't mean that you *should*. You'll still be saved, but not having works ruins your current life, prevents you from being able to help others, and you lose out on eternal rewards in Heaven.
How do I know for sure that James 2 is talking about the usefulness of your faith here on Earth and is not about salvation? The biggest clue is James 2:21:
> Was not Abraham our father justified by works, when he had offered Isaac his son upon the altar?
But then earlier in Romans 4:2-3 we have this:
> For if Abraham were justified by works, he hath whereof to glory; but not before God. For what saith the scripture? Abraham believed God, and it was counted unto him for righteousness.
So is this a contradiction? Was Abraham justified by works or by faith? Romans is talking about how Abraham's faith justified him before God and is what saved him, while James talks about how Abraham's works justified him before men, and that your faith should also be backed by works so that it can be useful to others in this life.
Even though works are not required for salvation, I believe that in most cases people will have some amount of works anyway. When you're saved, the Holy Spirit is with you, and when you sin after being saved, it's never the same as before. Your guilty conscience is much stronger, and you're more inclined to do the right thing. Not because you have to, but because you just want to.