Mac环境下使用@tensorflow/tfjs-node遇TypeError错误求助
解决方案:TypeError: (0 , util_1.isNullOrUndefined) is not a function
问题根源
该错误源于@tensorflow/tfjs-node依赖的内部工具方法在高版本Node.js(如23.x)中存在兼容性问题,或依赖包版本不匹配导致的模块调用失败。
具体解决步骤
锁定兼容的依赖版本
Node.js 23为开发预览版,tfjs-node尚未完全适配,建议降级到经过验证的稳定版本:- 删除现有依赖缓存:
rm -rf node_modules package-lock.json - 安装指定版本依赖:
npm install @tensorflow/tfjs-node@4.14.0 @tensorflow-models/mobilenet@2.1.1
- 删除现有依赖缓存:
替换decodeImage调用逻辑
绕过node模块的兼容性问题,改用tfjs通用的decodeImage方法,将buffer转为Uint8Array后传入:// 替换原有的tf.node.decodeImage调用 const imageTensor = tf.decodeImage(new Uint8Array(resizedImageBuffer), 3);降级Node.js到LTS版本
Node.js 23稳定性不足,建议切换到长期支持版(如20.x),使用nvm管理版本:nvm install 20 nvm use 20排查并修复依赖冲突
检查是否存在多版本tfjs-core冲突:npm ls @tensorflow/tfjs-core若存在冲突,在
package.json中添加版本锁定:"resolutions": { "@tensorflow/tfjs-core": "4.14.0" }安装
npm-force-resolutions后重新安装依赖:npm install npm-force-resolutions --save-dev npm install
内容的提问来源于stack exchange,提问作者AlbertMunichMar
相关产品推荐
相关产品推荐

