Rosetta ARM M1 - Intel npm

Nvm allows you to switch between different versions of node in the same machine

We use this trick to install one version, in arm architecture, and another in intel. So you can switch node versions easily with:

nvm use arm
node -p "process.arch" # arm64
nvm use intel
node -p "process.arch" # x64

Install nvm for intel with rosetta

Usually to install nvm you will use homebrew:

brew install nvm
Warning: nvm 0.39.1_1 is already installed and up-to-date.
To reinstall 0.39.1_1, run:
  brew reinstall nvm

But it's already installed with the arm64 M1 architecture which is not what we want.

We need nvm installed for the i386 Intel architecture.

Steps

Option 1

Open "Rosetta Terminal" (a duplicate of the normal Terminal, with "Open using Rosetta" ticked). If you do not already have it, you can set it up as described in this post.

Option 2

Alternatively, if you do not want to create a duplicate of your terminal, you can run the normal Terminal, and prefix each command with:

arch -x86_64 /bin/bash -c <your-command>

Back to our steps, I used the "Rosetta Terminal" which is the lazy way, Option 1 above.

Open "Rosetta Terminal", then make sure you are in i386 by typing:

arch
# i386

Then you can try to install nvm, but it won't work:

brew install nvm
Warning: nvm 0.39.1_1 is already installed and up-to-date.
To reinstall 0.39.1_1, run:
  brew reinstall nvm

Ok, let'st try what they say:

brew reinstall nvm
Error: Cannot install under Rosetta 2 in ARM default prefix (/opt/homebrew)!
To rerun under ARM use:
    arch -arm64 brew install ...
To install under x86_64, install Homebrew into /usr/local.

Still not. But the problem is well explained, we are using brew for the M1 chip, but we want to install a package with brew for the intel architecture, and mixing packages for both architectures under the same directory /opt/homebrew is not allowed.

Again, we will try the suggestion, and install an x64 architecture based brew in /usr/local/homebrew:

sudo su
cd /usr/local && mkdir homebrew
curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
exit
sudo chown -R $(whoami) /usr/local/homebrew

Now that we have a i386 architecture brew installed, let's try installing some packages

/usr/local/homebrew/bin/brew install nvm

If everything goes well we should do what they say, vim ~/.bash_profile add the following lines:

export NVM_DIR="$HOME/.nvm"
  [ -s "/usr/local/homebrew/opt/nvm/nvm.sh" ] && \. "/usr/local/homebrew/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/usr/local/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/usr/local/homebrew/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion

Exit and source ~/.bash_profile, then nvm should work.

Install node in both architectures using nvm

The process is outlined here very well. After this you can do:

nvm use arm
nvn use intel

to switch between the intel and arm architectures.