修复mac系统下node22安装模块报fatal error: ‘memory‘ file not found错误的问题

完整报错信息:

复制代码
>npm --verbose
npm ERR! code 1
npm ERR! path /Users/LAURIEN/Dev/ips-core-apis-internal-v2/node_modules/libpq
npm ERR! command failed
npm ERR! command sh -c node-gyp rebuild
npm ERR! CXX(target) Release/obj.target/addon/src/connection.o
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@10.0.1
npm ERR! gyp info using node@21.2.0 | darwin | arm64
npm ERR! gyp info find Python using Python version 3.11.5 found at "/opt/homebrew/opt/python@3.11/bin/python3.11"
npm ERR! gyp info spawn /opt/homebrew/opt/python@3.11/bin/python3.11
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args '/Users/LAURIEN/.nvm/versions/node/v21.2.0/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
npm ERR! gyp info spawn args 'binding.gyp',
npm ERR! gyp info spawn args '-f',
npm ERR! gyp info spawn args 'make',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args '/Users/LAURIEN/Dev/ips-core-apis-internal-v2/node_modules/libpq/build/config.gypi',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args '/Users/LAURIEN/.nvm/versions/node/v21.2.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args '/Users/LAURIEN/Library/Caches/node-gyp/21.2.0/include/node/common.gypi',
npm ERR! gyp info spawn args '-Dlibrary=shared_library',
npm ERR! gyp info spawn args '-Dvisibility=default',
npm ERR! gyp info spawn args '-Dnode_root_dir=/Users/LAURIEN/Library/Caches/node-gyp/21.2.0',
npm ERR! gyp info spawn args '-Dnode_gyp_dir=/Users/LAURIEN/.nvm/versions/node/v21.2.0/lib/node_modules/npm/node_modules/node-gyp',
npm ERR! gyp info spawn args '-Dnode_lib_file=/Users/LAURIEN/Library/Caches/node-gyp/21.2.0/<(target_arch)/node.lib',
npm ERR! gyp info spawn args '-Dmodule_root_dir=/Users/LAURIEN/Dev/ips-core-apis-internal-v2/node_modules/libpq',
npm ERR! gyp info spawn args '-Dnode_engine=v8',
npm ERR! gyp info spawn args '--depth=.',
npm ERR! gyp info spawn args '--no-parallel',
npm ERR! gyp info spawn args '--generator-output',
npm ERR! gyp info spawn args 'build',
npm ERR! gyp info spawn args '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! gyp info spawn make
npm ERR! gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
npm ERR! In file included from ../src/connection.cc:1:
npm ERR! In file included from ../src/addon.h:4:
npm ERR! In file included from ../../nan/nan.h:62:
npm ERR! In file included from /Users/LAURIEN/Library/Caches/node-gyp/21.2.0/include/node/node.h:73:
npm ERR! /Users/LAURIEN/Library/Caches/node-gyp/21.2.0/include/node/v8.h:21:10: fatal error: 'memory' file not found
npm ERR!    21 | #include <memory>
npm ERR!       |          ^~~~~~~~
npm ERR! 1 error generated.
npm ERR! make: *** [Release/obj.target/addon/src/connection.o] Error 1
npm ERR! gyp ERR! build error 
npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2
npm ERR! gyp ERR! stack at ChildProcess.<anonymous> (/Users/LAURIEN/.nvm/versions/node/v21.2.0/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:209:23)
npm ERR! gyp ERR! System Darwin 24.0.0
npm ERR! gyp ERR! command "/Users/LAURIEN/.nvm/versions/node/v21.2.0/bin/node" "/Users/LAURIEN/.nvm/versions/node/v21.2.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd /Users/LAURIEN/Dev/ips-core-apis-internal-v2/node_modules/libpq
npm ERR! gyp ERR! node -v v21.2.0
npm ERR! gyp ERR! node-gyp -v v10.0.1
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in: /Users/LAURIEN/.npm/_logs/2024-10-03T16_11_16_400Z-debug-0.log
Usage: npm <command>
(...)

错误原因分析:

错误发生在编译本地 C++ 扩展 deasync 时(node-gyp 调用 make),日志关键行:

fatal error: 'memory' file not found --- 表明 C++ 标准头文件(例如 <memory>)不可见给编译器。

常见导致原因:

macOS 的 C/C++ 标准库头文件/SDK 没有安装或命令行工具路径未配置正确(Xcode Command Line Tools / Xcode)。

Xcode 许可未接受或 xcode-select 指向不正确的位置。

Node 版本与该包/预编译二进制不兼容,导致回退到本地编译(如果本机缺少编译环境就会失败)。

在 Apple Silicon(arm64)与 Node 新版本下,某些包还没有预编译二进制。

解决方法:

  1. 检查并安装 Xcode 命令行工具(若未安装)
  • 检查安装状态:
bash 复制代码
xcode-select -p
  • 如果没有安装或路径不正确,运行:
bash 复制代码
xcode-select --install
  1. 确认 xcode-select 指向正确的工具链并接受许可
bash 复制代码
# 指向 Command Line Tools(如果你安装了 full Xcode,可指向 /Applications/Xcode.app/Contents/Developer)
sudo xcode-select -s /Library/Developer/CommandLineTools
  1. 确保 SDKROOT 环境变量可被 node-gyp 识别(有时需要显式导出)
bash 复制代码
export SDKROOT="$(xcrun --sdk macosx --show-sdk-path)"
# 验证
echo $SDKROOT
  1. 清理并重建本地模块
bash 复制代码
# 在项目根目录执行
rm -rf node_modules
rm -f package-lock.json
npm cache clean --force

# 然后重新安装
npm install
  1. 若仍失败,尝试使用较稳定/兼容的 Node 版本(如 Node 18)
相关推荐
没事别瞎琢磨24 分钟前
十一、审计与 Run Session——每一步操作都被记录
人工智能·node.js
没事别瞎琢磨25 分钟前
十六、AgentSandbox——把所有模块串起来的编排类
人工智能·node.js
没事别瞎琢磨29 分钟前
十二、网络代理与白名单规则引擎
人工智能·node.js
没事别瞎琢磨33 分钟前
十四、Git Worktree 隔离执行
人工智能·node.js
没事别瞎琢磨2 小时前
十、统一 Runner 入口——能力检测与模式回退
人工智能·node.js
Tr2e2 小时前
🐱 从 0 到 1:用 Swift 手搓一个 macOS 桌面宠物(附源码)
macos·ios·swift
没事别瞎琢磨2 小时前
八、环境隔离——构建安全的子进程环境
人工智能·node.js
没事别瞎琢磨3 小时前
六、输出捕获与截断
人工智能·node.js
没事别瞎琢磨3 小时前
七、敏感路径预检——Protected Paths
人工智能·node.js
没事别瞎琢磨3 小时前
五、进程执行——spawn、超时与进程树清理
人工智能·node.js