Node 19.3.0 causing Sanity build error with webpack module parsing
This is a classic Next.js + Sanity configuration issue that's been reported before. The error you're seeing happens because Next.js needs to transpile certain Sanity packages (specifically @sanity/ui) that are distributed as ESM (ES Module) files.
Based on the community discussion around this exact issue, there are two solutions:
Solution 1: Configure transpilePackages (Recommended)
Add transpilePackages to your next.config.js or next.config.mjs:
/** @type {import('next').NextConfig} */
const config = {
transpilePackages: ['@sanity/ui', '@sanity/icons'],
// ... rest of your config
}
module.exports = configOr if using next.config.mjs:
/** @type {import('next').NextConfig} */
const config = {
transpilePackages: ['@sanity/ui', '@sanity/icons'],
// ... rest of your config
}
export default configThis tells Next.js to transpile these specific packages during the build process, which resolves the "sourceType: module" error.
Solution 2: Pin @sanity/ui version (Temporary workaround)
If you're using Yarn, you can add a resolution to your package.json:
"resolutions": {
"@sanity/ui": "1.0.5"
}Then delete your node_modules and yarn.lock, and run yarn install again. This was the workaround suggested when the issue first appeared with @sanity/ui@1.0.6.
About your Node version
You mentioned running Node 19.3.0 - this could be contributing to the issue. Node 19 is an odd-numbered release that's no longer maintained. I'd strongly recommend switching to:
- Node 18 LTS (minimum for Sanity Studio v3)
- Node 20 LTS (required for Studio v4, which you'll need eventually)
Why it works on Vercel but not locally
Vercel's build environment may have different default configurations or webpack settings that handle ESM imports differently. The transpilePackages configuration ensures consistent behavior across all environments.
After making these changes, delete your .next folder and run yarn build again. The transpilePackages solution is the proper long-term fix, while the version pinning is just a temporary workaround.
Show original thread13 replies
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.