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

Sanity Client配置报错:TypeError: Object is not a function

解决Sanity客户端连接的TypeError问题

问题详情

本地搭建Sanity连接时,配置client.js后出现以下错误:

TypeError: Object is not a function. (In 'Object(get_it_middleware__WEBPACK_IMPORTED_MODULE_14__["retry"])','Object' is an instance of Object)

client.js代码:

import { createClient } from "@sanity/client";

const client = createClient ({
  projectId: 'xxx-xxx-xxx',
  dataset: 'production',
  apiVersion: '2023-07-28',
  token: '',
  useCdn: true, 
})

export default client;

报错位置(node_modules/@sanity/client/dist/index.browser.js:141):

138 | var _ref$maxRetries = _ref.maxRetries,
  139 |   maxRetries = _ref$maxRetries === void 0 ? 5 : _ref$maxRetries,
  140 |   retryDelay = _ref.retryDelay;
> 141 | var request = getIt([maxRetries > 0 ? retry({
      | ^  142 |   // eslint-disable-next-line @typescript-eslint/no-explicit-any
  143 |   retryDelay: retryDelay,
  144 |   // This option is typed incorrectly in get-it.

已尝试重新安装@sanity/client及旧版本,问题未解决。

解决方案

  • 清理依赖缓存并重新安装
    删除node_modules文件夹、package-lock.json(或yarn.lock),然后执行:

    npm install
    # 或使用yarn
    yarn install
    

    这能解决大部分依赖版本冲突或缓存导致的问题。

  • 锁定get-it依赖版本
    错误源于@sanity/client依赖的get-it库中retry中间件异常,可在package.json中指定兼容版本:

    "resolutions": {
      "get-it": "^8.1.0"
    }
    

    再重新安装依赖。

  • 降级到@sanity/client稳定版本
    尝试降级到已知无此问题的版本,比如6.1.3:

    npm install @sanity/client@6.1.3
    
  • 检查Webpack配置
    若项目用Webpack,避免异常转译node_modules模块,可在webpack.config.js中调整:

    module.exports = {
      // ...其他配置
      module: {
        rules: [
          {
            test: /\.js$/,
            exclude: /node_modules/,
            use: 'babel-loader'
          }
        ]
      }
    }
    
  • 切换导入方式
    若项目使用CommonJS环境,改用require导入:

    const { createClient } = require("@sanity/client");
    
    const client = createClient({
      projectId: 'xxx-xxx-xxx',
      dataset: 'production',
      apiVersion: '2023-07-28',
      token: '',
      useCdn: true, 
    })
    
    module.exports = client;
    

内容的提问来源于stack exchange,提问作者JazzyFunk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 23:47:48