搭载M1芯片的2020款MacBook Air上bcrypt Node.js模块无法运行问题求助
Hey there, this is a super common pain point when working with native Node modules like bcrypt on M1/M2 Macs—let's get this sorted out for you.
First, let's break down the core issue from your error log:
Error: dlopen(/Users/jaberghanem/Desktop/innentialApp/backend/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node, 0x0001): tried: '/Users/jaberghanem/Desktop/innentialApp/backend/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')) ... [nodemon] app crashed - waiting for file changes before starting...
This means the bcrypt native binary was compiled for your M1's ARM64 architecture, but your Node.js runtime is running in x86_64 mode (most likely via Rosetta 2). The two architectures don't line up, hence the crash.
Here are the most reliable fixes to try:
Option 1: Reinstall bcrypt using Rosetta Terminal
If you rely on Rosetta for compatibility with other tools, rebuild bcrypt to match the x86_64 environment:
- Close all open terminal windows
- Go to Applications → Utilities, right-click Terminal, select Get Info
- Check Open using Rosetta, then close the info window
- Open a new terminal, navigate to your project backend:
cd /Users/jaberghanem/Desktop/innentialApp/backend - Clean up existing dependencies:
rm -rf node_modules package-lock.json - Reinstall dependencies:
npm install - If the error sticks, force a source rebuild of bcrypt:
npm rebuild bcrypt --build-from-source
Option 2: Use ARM64-native Node.js (no Rosetta needed)
For better performance, run everything natively on your M1's architecture:
- First, ensure your terminal is NOT running in Rosetta (uncheck the box in Terminal's Get Info if enabled)
- If you use
nvm, install an ARM64-compatible LTS Node version (16+ works great):nvm install 18 nvm use 18 - Clean up old dependencies:
rm -rf node_modules package-lock.json - Reinstall:
npm install
Option 3: Quick force reinstall
If you need a fast fix, force bcrypt to recompile for your current runtime:
npm uninstall bcrypt npm install bcrypt --force
Pro Tips:
- Never mix ARM64 and x86_64 environments for Node and native modules—this is the #1 cause of architecture mismatch errors.
- Stick to the latest stable bcrypt version (v5.x as of now) since older releases have limited M1 support.
内容的提问来源于stack exchange,提问作者jaber

