Skip to content

ESM isn't published correctly #127

@dburles

Description

@dburles

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:

  1. Rename all ESM exports to .mjs, and CommonJS exports to .cjs (Recommended).
    or
  2. Set package.json type as "module" (so Node then treats .js as 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreleased

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions