-
Notifications
You must be signed in to change notification settings - Fork 107
Closed
Labels
Description
As there's no type field set in package.json, Node will assume anything with a .js file extension to be CommonJS and treats the exported ESM as CommonJS.
Reproduction:
> npm i next-sanity
> echo 'import { groq } from "next-sanity";' > test.mjs
node test.mjs
Results in:
import { groq } from "next-sanity";
^^^^
SyntaxError: Named export 'groq' not found. The requested module 'next-sanity' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from 'next-sanity';
const { groq } = pkg;
Solution:
- Rename all ESM exports to
.mjs, and CommonJS exports to.cjs(Recommended).
or - Set
package.jsontypeas "module" (so Node then treats.jsas ESM) and then rename all extensions of commonjs exports to.cjs.
Also, if the package is to remain publishing dual bundles, you need to ensure that there's no dual package hazard.
For more information see the Node.js docs: https://nodejs.org/api/packages.html#dual-commonjses-module-packages