Setting up M1/M2 Mac for using package node-gyp

steckdev

Tyler Steck

Posted on March 2, 2023

Setting up M1/M2 Mac for using package node-gyp

If you're using an M1 Mac and trying to use Node.js for native module development, you may run into errors with node-gyp. Node-gyp is a tool that compiles native modules for Node.js, and it needs to be configured correctly to work on an M1 Mac. In this article, we'll explore how to set up an M1 Mac to use node-gyp without errors.

Step 1: Install Command Line Tools

After installing Xcode, you need to install the Command Line Tools. Open a terminal window and enter the following command:

xcode-select --install
Enter fullscreen mode Exit fullscreen mode

This command installs the Command Line Tools, which include the necessary tools for compiling software on a Mac.

Step 2: Install Homebrew

Homebrew is a package manager for macOS that makes it easy to install and manage software packages. You can install Homebrew by entering the following command in a terminal window:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

This command installs Homebrew and sets it up on your system.

Step 3: Install Node.js

After installing Homebrew, you can use it to install Node.js. Enter the following command in a terminal window:

brew install node
Enter fullscreen mode Exit fullscreen mode

This command installs the latest version of Node.js and its dependencies.

Step 4: Configure node-gyp

Now that Node.js is installed, you need to configure node-gyp to work on an M1 Mac. Open a terminal window and enter the following command:

export CXXFLAGS="-stdlib=libc++"
Enter fullscreen mode Exit fullscreen mode

This command sets the C++ standard library to libc++, which is required for node-gyp to work on an M1 Mac.

Step 5: Test node-gyp

To test that node-gyp is configured correctly, you can install a native module that depends on it. Open a terminal window and enter the following command:

npm install -g node-gyp
Enter fullscreen mode Exit fullscreen mode

This command installs the node-gyp package globally.

To test that node-gyp is working correctly, you can create a new Node.js project and install a native module that depends on it. For example, you can create a new project with the following command:bash

mkdir test-project && cd test-project
npm init -y
Enter fullscreen mode Exit fullscreen mode

Then, you can install a native module that depends on node-gyp with the following command:

npm install sharp
Enter fullscreen mode Exit fullscreen mode

The sharp module is an image processing library that uses node-gyp to compile native code.

If node-gyp is configured correctly, you should be able to install the sharp module without errors.

Conclusion

Setting up node-gyp on an M1 Mac can be challenging, but it's necessary for native module development in Node.js. By following these steps, you can configure node-gyp to work correctly on your M1 Mac and avoid errors when compiling native modules.

πŸ’– πŸ’ͺ πŸ™… 🚩
steckdev
Tyler Steck

Posted on March 2, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related