Skip to content
Myr

[readme] clarify instructions for running Node on M1 Mac

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Luke LaFountaine · committed by Jordan Harband · parent 79ad72d · Browse files · 1 file+59 added−46 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+59 added−46 removed

  • other:+59 added−46 removed

1 file+59 added−46 removed

modified:README.md+59 added-46 removed
901 unmodified lines
902Homebrew causes insecure directories like `/usr/local/share/zsh/site-functions` and `/usr/local/share/zsh`. This is **not** an `nvm` problem - it is a homebrew problem. Refer [here](https://github.com/zsh-users/zsh-completions/issues/680) for some solutions related to the issue.903904**Macs with M1 chip**905906_January 2021:_ there are no pre-compiled NodeJS binaries for versions prior to 15.x for Apple's new M1 chip (arm64 architecture).906Experimental support for the M1 architecture was added in node.js v15.3 and full support was added in v16.0.907Because of this, if you try to install older versions of node as usual, you will probably experience either compilation errors when installing node or out-of-memory errors while running your code.908908Some issues you may encounter:909So, if you want to run a version prior to v16.0 on an M1 Mac, it may be best to compile node targeting the x86_64 Intel architecture so that Rosetta 2 can translate the x86_64 processor instructions to ARM-based Apple Silicon instructions.910Here's what you will need to do:911910- using `nvm` to install, say, `v14.15.4`:911 - the C code compiles successfully912 - but crashes with an out of memory error when used913 - increasing the memory available to node still produces the out of memory errors:914 ```sh915 $ NODE_OPTIONS="--max-old-space-size=4096" ./node_modules/.bin/your_node_package916 ```917- when using `nvm` to install some versions, the compilation fails918- after `nvm` successfully compiles some versions, `yarn` or `npm` may later fail to install packages with an `incorrect data check` error.912- Install Rosetta, if you haven't already done so913920One solution to this issue is to change the architecture of your shell from arm64 to x86.914 ```sh915 $ softwareupdate --install-rosetta916 ```917922Let's assume that:923- you already have versions `12.20.1` and `14.15.4` installed using `nvm`924- the current version in use is `14.15.4`925- you are using the `zsh` shell926- you have Rosetta 2 installed (macOS prompts you to install Rosetta 2 the first time you open a Intel-only non-command-line application, or you may install Rosetta 2 from the command line with `softwareupdate --install-rosetta`)918 You might wonder, "how will my M1 Mac know to use Rosetta for a version of node compiled for an Intel chip?".919 If an executable contains only Intel instructions, macOS will automatically use Rosetta to translate the instructions.920928```sh929# Check what version you're running:930$ node --version931v14.15.4932# Check architecture of the `node` binary:933$ node -p process.arch934arm64935# This confirms that the arch is for the M1 chip, which is causing the problems.936# So we need to uninstall it.937# We can't uninstall the version we are currently using, so switch to another version:938$ nvm install v12.20.1939# Now uninstall the version we want to replace:940$ nvm uninstall v14.15.4941# Launch a new zsh process under the 64-bit X86 architecture:942$ arch -x86_64 zsh943# Install node using nvm. This should download the precompiled x64 binary:944$ nvm install v14.15.4945# Now check that the architecture is correct:946$ node -p process.arch947x64948# It is now safe to return to the arm64 zsh process:949$ exit950# We're back to a native shell:951$ arch952arm64953# And the new version is now available to use:954$ nvm use v14.15.4955Now using node v14.15.4 (npm v6.14.10)956```921- Open a shell that's running using Rosetta922923 ```sh924 $ arch -x86_64 zsh925 ```926927 Note: This same thing can also be accomplished by finding the Terminal or iTerm App in Finder, right clicking, selecting "Get Info", and then checking the box labeled "Open using Rosetta".928929 Note: This terminal session is now running in `zsh`.930 If `zsh` is not the shell you typically use, `nvm` may not be `source`'d automatically like it probably is for your usual shell through your dotfiles.931 If that's the case, make sure to source `nvm`.932933 ```sh934 $ source "${NVM_DIR}/.nvm/nvm.sh"935 ```936937- Install whatever older version of node you are interested in. Let's use 12.22.1 as an example.938 This will fetch the node source code and compile it, which will take several minutes.939940 ```sh941 $ nvm install v12.22.1 --shared-zlib942 ```943944 Note: You're probably curious why `--shared-zlib` is included.945 There's a bug in recent versions of Apple's system `clang` compiler.946 If one of these broken versions is installed on your system, the above step will likely still succeed even if you didn't include the `--shared-zlib` flag.947 However, later, when you attempt to `npm install` something using your old version of node.js, you will see `incorrect data check` errors.948 If you want to avoid the possible hassle of dealing with this, include that flag.949 For more details, see [this issue](https://github.com/nodejs/node/issues/39313) and [this comment](https://github.com/nodejs/node/issues/39313#issuecomment-902395576)950951- Exit back to your native shell.952953 ```sh954 $ exit955 $ arch956 arm64957 ```958959 Note: If you selected the box labeled "Open using Rosetta" rather than running the CLI command in the second step, you will see `i386` here.960 Unless you have another reason to have that box selected, you can deselect it now.961962- Check to make sure the architecture is correct. `x64` is the abbreviation for x86_64, which is what you want to see.963964 ```sh965 $ node -p process.arch966 x64967 ```968969Now you should be able to use node as usual.970971## Maintainers972973Currently, the sole maintainer is [@ljharb](https://github.com/ljharb) - more maintainers are quite welcome, and we hope to add folks to the team over time. [Governance](./GOVERNANCE.md) will be re-evaluated as the project evolves.974
9 unmodified lines