In Dummit & Foote's Abstract Algebra, pg 460, Chap 12, they defined:
For any integral domain $R$ the rank of an $R$-module $M$ is the maximum number of $R$-linearly independent elements of $M$.
Sorry if these are obvious:
(i) If the maximum number is not finite does this mean we define the rank as: the cardinality of a maximal $R$-linearly independent set.
(ia) But then we would have to show all maximal $R$-linearly independent set have same cardinality?
(ib) How do we know such a maximal $R$-linearly independent set of elements in $M$ exists?
(i) is the correct definition.
(ia) follows from the fact, that if $U \subset M$ is a maximal $R$-linearly independent subset, then $U$ is a basis of $M \otimes K$, where $K$ is the fraction field of $R$. Thus we get the result from the well known result in linear algebra. This also implies that the rank is nothing but $\dim_K M \otimes K$. In many sources you will find this as a definition.
(ib) is a consequence of Zorn's Lemma. This is essentially the same proof as the proof that any vector space admits a basis.