Skip to content

require of package with CJS exports.require and ESM main fails with ERR_REQUIRE_ESM after any quibble #110

Closed
@lukekarrys

Description

@lukekarrys

A dependency with type: "module" and conditional exports where the default is ESM and the require is CJS a CJS exports["."].require and an ESM main will fail with an ERR_REQUIRE_ESM error after any call to quibble().

This reproduces for me with the latest version of quibble (0.9.2) and the latest version of Node 18 (18.20.1) an 20 (20.12.1).

Reproduction

Save the following bash script as repro.sh to an empty directory and run it with bash repro.sh:

#!/usr/bin/env bash

rm -rf index.js
rm -rf package.json
rm -rf package-lock.json
rm -rf node_modules/
rm -rf dep-with-conditional-exports/

[[ "$1" == "reset" ]] && exit 0

# Create the dep with conditional exports
mkdir dep-with-conditional-exports
echo "module.exports = 'cjs'" > ./dep-with-conditional-exports/index.cjs
echo "export default 'esm'" > ./dep-with-conditional-exports/index.js
cat << EOF > ./dep-with-conditional-exports/package.json
{
  "name": "dep-with-conditional-exports",
  "type": "module",
  "main": "index.js",
  "exports": {
    "require": "./index.cjs",
    "default": "./index.js"
  }
}
EOF

# Create root package json and install it
cat << EOF > ./package.json
{
  "name": "require-conditional-exports-after-quibble",
  "version": "1.0.0",
  "dependencies": {
    "dep-with-conditional-exports": "file:./dep-with-conditional-exports",
    "quibble": "^0.9.2"
  }
}
EOF
npm install --no-progress --silent

# Require conditional exports dep
echo "Can be required:"
node -e 'console.log(require("dep-with-conditional-exports"))'
echo ""

# Import conditional exports dep
echo "Can be imported:"
node -e 'import("dep-with-conditional-exports").then(r => console.log(r.default))'
echo ""

# Require it after an empty quibble
cat << EOF > ./index.js
require('quibble')()
console.log(require('dep-with-conditional-exports'))
EOF
echo "Can be required after quibble:"
node index.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions