Sanity start fails: "Cannot find package 'next'" error in schema.js

6 replies
Last updated: Nov 25, 2022
Hi everyone!
A few days everything was working finde when I used the command


sanity start
but when I tried to run it today, I am getting an error:


✔ Checking configuration files...
⠦ Compiling...webpack built 13b315e7094243c5af96 in 10658ms
✔ Compiling...
Failed to compile.

Error in ./schemas/schema.js (part:@sanity/base/schema)
Module build failed: Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'next' imported from /home/user/Documents/dci/my-projects/myPortfolio/portfolio-sanity/babel-virtual-resolve-base.js
    at Generator.next (<anonymous>)
    at new Promise (<anonymous>)
 @ ./node_modules/@sanity/default-layout/lib/schemaErrors/SchemaErrorReporter.js 8:37-72
 @ ./node_modules/@sanity/default-layout/lib/defaultLayout/DefaultLayout.js
 @ ./node_modules/@sanity/default-layout/lib/defaultLayout/index.js
 @ ./node_modules/@sanity/default-layout/lib/Root.js (part:@sanity/base/root)
 @ ./node_modules/@sanity/base/lib/components/SanityRoot.js (part:@sanity/base/sanity-root)
 @ ./node_modules/@sanity/server/lib/browser/entry-dev.js
 @ multi ./node_modules/@sanity/server/lib/browser/entry-dev.js
I am still quite new in it, so could anyone explain me please how to resolve it.
Thanks in advance!!!
AI Update

This error occurs because your Sanity Studio's build process is trying to import the next package, but it's not installed in your Studio directory. This typically happens when there's a configuration mismatch or when you've accidentally imported something from Next.js in your Studio schema files.

Here are the steps to resolve this:

1. Check your schema.js file for Next.js imports

Look in schemas/schema.js (and any files it imports) for any lines that might be importing from 'next':

import something from 'next'
// or
import { something } from 'next/...'

Sanity Studio and Next.js are separate applications - your Studio schema files shouldn't import anything from Next.js. If you find any such imports, remove them.

2. Check for accidental file mixing

Make sure you haven't accidentally copied code meant for your Next.js frontend into your Sanity Studio directory. The Studio should be in its own folder (often called studio/ or sanity/) separate from your Next.js app.

3. Clear cache and reinstall

Sometimes build caches can cause issues. Try:

# Remove node_modules and lock file
rm -rf node_modules
rm package-lock.json  # or yarn.lock if using yarn

# Reinstall dependencies
npm install

# Try starting again
sanity start

4. If you actually need Next.js in the same project

If you're working in a monorepo where both Studio and Next.js coexist, make sure they're in separate directories with their own package.json files, and you're running sanity start from the correct Studio directory.

The key insight here is that Sanity Studio is a React application that runs independently from your Next.js frontend. They should have separate dependencies and shouldn't cross-import from each other's packages. Your schema files should only use Sanity-specific imports.

If you're still stuck after trying these steps, share the contents of your schemas/schema.js file and your project structure - that would help identify where the Next.js reference is coming from.

Show original thread
6 replies
It looks like you’re trying to import a package in your
schema.js
file that doesn’t exist
Are you able to share the code from the
./schemas/schema.js
file?
import createSchema from 'part:@sanity/base/schema-creator'
import schemaTypes from 'all:part:@sanity/base/schema-type'
import home from './home'
import about from './about'
import project from './project'

export default createSchema({
  name: 'default',
  types: schemaTypes.concat([
    home,
    about,
    project,
  ]),
})
I have not changed anything in this file since last time.
user J
I have even reinstalled all the packages but still not working.I am getting confused as well
user J
Thanks a lot it worked!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?