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

运行index.js代码时遇Vite内部错误:无法解析Sanity导入模块

解决Sanity Schema导入错误:无法解析part:@sanity/base/schema-creator

这个错误的核心原因很明确——part:@sanity/base/schema-creator这类导入是Sanity Studio专属的内部模块约定,并不是普通的npm包,直接用Vite或node命令运行schema文件,自然没法解析这些特殊路径。下面是具体的解决步骤:

1. 使用正确的项目启动方式

如果这是一个Sanity Studio项目,绝对不能直接用Vite或node执行schemas/index.js。你需要用Sanity官方CLI命令启动开发服务器,它能自动处理这类内部模块的解析:

sanity start

2. 检查Sanity版本(关键排查点)

这个报错大概率是混用了Sanity v2和v3的写法导致的:

  • Sanity v2:确实使用part:@sanity/base/schema-creator这类导入,但要确保你安装了完整的v2依赖,并且始终用sanity start启动项目。
  • Sanity v3:已经彻底废弃了part:@开头的导入方式,改为直接从sanity包导入。你需要把旧代码替换成v3的写法:

原v2代码:

import createSchema from 'part:@sanity/base/schema-creator';
import schemaTypes from 'all:part:@sanity/base/schema-type';

替换为v3写法:

import { defineConfig, SchemaTypeDefinition } from 'sanity';

// 定义你的schema类型
const postSchema: SchemaTypeDefinition = {
  name: 'post',
  type: 'document',
  fields: [
    { name: 'title', type: 'string' },
    { name: 'content', type: 'text' }
  ]
};

export default defineConfig({
  projectId: '你的项目ID',
  dataset: 'production',
  schema: {
    types: [postSchema],
  },
});

3. 验证依赖安装

如果是v2项目,确保安装了所有必要的依赖:

npm install @sanity/base @sanity/core sanity

如果是v3项目,只需要安装核心包即可:

npm install sanity

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 16:45:29