Problem
Recently, when using yarn install or npm install to install Node.js packages that require native compilation (such as node-sass, bcrypt, or other packages with native dependencies), I encountered the following error:
gyp info spawn args ]
Traceback (most recent call last):
File "/Users/camel/cm-next/node_modules/node-gyp/gyp/gyp_main.py", line 42, in <module>
import gyp # noqa: E402
^^^^^^^^^^
File "/Users/camel/cm-next/node_modules/node-gyp/gyp/pylib/gyp/__init__.py", line 9, in <module>
import gyp.input
File "/Users/camel/cm-next/node_modules/node-gyp/gyp/pylib/gyp/input.py", line 19, in <module>
from distutils.version import StrictVersion
ModuleNotFoundError: No module named 'distutils'
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
This error occurs because Python 3.12+ removed the distutils module, which node-gyp relies on for building native Node.js modules.
Solution
Install python-setuptools to resolve the distutils module error:
brew install python-setuptools
This will install the setuptools package, which includes the distutils module, allowing node-gyp to function correctly. 🎉