运行在 Apple 芯片下的 macOS 中的 Node.JS 编译

Posted by

on

前言

需要在使用 M1 芯片的 Mac 中使用原生(arm64)的 Node.JS。截至写下这篇文章的时间(2021/3/18),Node.JS 官网还没有可以直接使用的 Bin 包。

准备工作

  1. 安装 Homebrew
  2. 安装 nodebrew
    brew install -s nodebrew

编译安装

    1. 由于 nodebrew 在写下这篇文章的时间还没有正确识别 arm64 架构的代码,我们需要手动修改 nodebrew 脚本。
    2. nano $(which nodebrew)
    3. 找到 system_info 并改成如下所示的样子:
      sub system_info {
          my $arch;
          my ($sysname, $machine) = (POSIX::uname)[0, 4];
      
          if  ($machine =~ m/x86_64|arm64/) {
              $arch = 'arm64';
    4. 使用 nodebrew compile v<版本号> 编译。在当前时间节点,建议不要使用 LTS 版本,以避免可能存在的问题。
    5. 如果存在提示 No such file or directory 导致无法拉取 Node.JS 源代码的,可以手动建立目录。
      mkdir -p /Users/用户名/.nodebrew/src/
    6. 正常编译:Apple 芯片的机器编译 Node.JS
    7. 使用 nodebrew use v<版本号> 选取 Node.JS。
    8. 设置环境变量。
      echo "export PATH=$HOME/.nodebrew/current/bin:$PATH" >> ~/.zshrc

参考资料

  1. M1 Mac を購入して arm64 縛りでインストールしたもの (更新中), https://codehex.hateblo.jp/entry/2020/12/03/194230, アルパカ三銃士, 2020-12-03
  2. 会社の支給PCがMacBook Pro M1なので、新しく開発環境を構築した話, https://tech.tabechoku.com/entry/2021/01/15/150103, 食べチョク開発者ブログ, 2021-01-15

Leave a Reply

Your email address will not be published. Required fields are marked *