You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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尚未完全适配,建议降级到经过验证的稳定版本:

    1. 删除现有依赖缓存:
      rm -rf node_modules package-lock.json
      
    2. 安装指定版本依赖:
      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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 06:32:37