ricokahler / sanity-codegen

Generate TypeScript types from your Sanity.io schemas
sanity-codegen-dev.vercel.app
MIT License
270 stars 19 forks source link

Error: Cannot find module '../schemas' #266

Open ollebergkvist opened 2 years ago

ollebergkvist commented 2 years ago

I'm getting this error running Next, sanity blog example with the codegen.

sanity-codegen.config.ts

const config: SanityCodegenConfig = {
  schemaPath: "./blog/schemas",
  outputPath: "./schema.ts",

  // NOTE: The CLI ships with a pre-configured babel config that shims out
  // the Sanity parts system. This babel config does not read from any
  // `.babelrc` or `babel.config.js`. You can only configure extra babel
  // options here.
  // babelOptions: require('./.babelrc.json'), // (optional)
};

Error message:

npx sanity-codegen                   
Error: Cannot find module '/Users/user/Documents/dev/sanity/sanity-codegen-test/blog/schemas'
Require stack:
- /Users/user/Documents/dev/sanity/sanity-codegen-test/node_modules/sanity-codegen/cli.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at cli (/Users/user/Documents/dev/sanity/sanity-codegen-test/node_modules/sanity-codegen/cli.js:100:17) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/user/Documents/dev/sanity/sanity-codegen-test/node_modules/sanity-codegen/cli.js'
  ]
}

Dependencies:

"dependencies": {
    "next": "12.1.6",
    "next-sanity": "^0.5.2",
    "prettier": "^2.6.2",
    "react": "18.1.0",
    "react-dom": "18.1.0",
    "sanity": "^2.29.3",
  },

"devDependencies": {
    "@babel/core": "^7.18.0",
    "@babel/preset-env": "^7.18.0",
    "@babel/preset-react": "^7.17.12",
    "@babel/preset-typescript": "^7.17.12",
    "@sanity/types": "^2.29.5",
    "eslint": "8.15.0",
    "eslint-config-next": "12.1.6",
    "jsdom": "^19.0.0"
    "sanity-codegen": "^0.9.8"
  }
ollebergkvist commented 2 years ago

I tried to run the package with the official Vercel example as well, same issue:

https://github.com/vercel/next.js/tree/canary/examples/cms-sanity
eldoy commented 2 years ago

Use this line in the config:

schemaPath: './studio/schemas/schema.js',

The script is looking for the file where you run createSchema, just read the source. This fixed it for me.

ollebergkvist commented 2 years ago

I added the path to the schema file as well, which now leads to:

/Users/user/Documents/dev/project/snap-system/apps/cms/schemas/schema.ts:1
import schemaTypes from 'all:part:@sanity/base/schema-type'
^^^^^^

SyntaxError: Cannot use import statement outside a module
adebiyial commented 2 years ago

@ollebergkvist I think it should be /Users/user/Documents/dev/project/snap-system/apps/cms/schemas/schema

DuEyE commented 2 years ago

I got it working in my setup. Run npx sanity-codegen in the sanity directory. My utils folder and the babel.config are in the root of the project. ` import { SanityCodegenConfig } from 'sanity-codegen';

const config: SanityCodegenConfig = { schemaPath: './schemas/schema.js', outputPath: '../utils/types/types.ts',

// NOTE: The CLI ships with a pre-configured babel config that shims out // the Sanity parts system. This babel config does not read from any // .babelrc or babel.config.js. You can only configure extra babel // options here. babelOptions: require('../babel.config.js'), // (optional) };

export default config; `