Dynamic import() ES module error in Node.js prebuild script
This is actually a Node.js module system issue, not specifically a Sanity problem, but I can help clarify what's happening here.
You're correct that Node.js supports dynamic import() in CommonJS files - but there's a critical detail: the file you're importing (../config.js) must be recognized as an ES module by Node.js.
The error message is telling you that config.js contains ES module syntax (export), but Node.js is trying to parse it as CommonJS because:
- Your
package.jsondoesn't have"type": "module", AND - The file doesn't have a
.mjsextension
Solutions:
Option 1: Make config.js recognizable as ESM
- Rename
config.jstoconfig.mjs, OR - Add
"type": "module"to yourpackage.json
Option 2: Keep everything CommonJS
If prebuild.js needs to stay as CommonJS (.js without "type": "module"), convert config.js to use CommonJS exports:
// config.js
const config = {
// your config
};
module.exports = { config };Then in prebuild.js:
// prebuild.js
(async function () {
const { createClient } = require("next-sanity");
const { config } = require("../config.js");
})();Option 3: Make prebuild.js an ES module
Rename to prebuild.mjs and use ESM throughout:
// prebuild.mjs
import { createClient } from "next-sanity";
import { config } from "../config.js";The Node.js docs are correct - dynamic import() works in CommonJS, but the target file still needs to be identifiable as an ES module through file extension or package.json configuration. Node.js can't determine module type purely from file contents when using .js extension without package.json hints.
Show original thread4 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.