My argument is as follows:
Let $R$ be a commutative ring with unity, $I$ an ideal of $R$. If $(R/I)^n\cong (R/I)^m$ as $R$-modules, then it follows that they are isomorphic as $R/I$-modules because the isomorphism factors through the quotient. We observe that these are both free $R/I$-modules with bases $${\mathfrak{B}_1=\{\delta_{1j}+I, \delta_{2j}+I, \dots, \delta_{nj}+I\} \;\;\text{ and }\;\; \mathfrak{B}_2=\{\delta_{1j}+I, \delta_{2j}+I, \dots, \delta_{mj}+I\}}$$ respectively. Then the isomorphism between $(R/I)^n \cong (R/I)^m$ as $R$-modules induces an isomorphism of these free modules, meaning there is a bijection between the elements of $\mathfrak{B}_1$ and $\mathfrak{B}_2$. It follows then that $n=m$.
More generally, you've essentially showed the following:
Indeed, the $R$-module isomorphism extends to an $S$-module isomorphism $S^m \otimes_R S\cong S^n \otimes_R S$. Since tensor products commute with direct sums, you get an isomorphism $(S \otimes_R S)^m \cong (S \otimes_R S)^n$. Since $R \rightarrow S$ is an epimorphism, the multiplication map $S \otimes_R S \rightarrow S$ is an isomorphism. Finally conclude $m = n$ because commutative rings have IBN (do make sure that you understand that part of the argument. As Captain Lama noted, your reasoning there was faulty).
This naturally raises the question
Yes, it can. Consider for example any ring $R$ and the ring $S = R^\mathbb{N}$ (product of countably many copies of $\mathbb{N}$).
We have an $R$-module isomorphism $S^2 \cong S$ given by $\big((r_1, r_2, \ldots), (q_1, q_2, \ldots) \big) \leadsto (r_1, q_1, r_2, q_2, \ldots)$.