Say $M$ is a finitely generated module over a domain $R$.
Define a function $\mathrm{rk}_M \colon \mathrm{Spec} ( R) \to \mathbb{N}$ by setting $\mathrm{rk}_M(P)$ equal to the dimension of $M_P / PM_P$ as a $R_P / PR_P$ vector space.
It is well know that if $\mathrm{rk}_M$ is not constant on $\mathrm{Spec} (R)$, then $M$ is not projective, since $R$ is a domain.
I am curious about the converse: if $M$ is not projective, can we guarantee that $\mathrm{rk}_M$ is not constant?
If the converse is not true, are there standard examples which show this?