Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ERR_REQUIRE_ESM error under CommonJS module system #162

Merged
merged 3 commits into from
Dec 19, 2024

Conversation

hzrd149
Copy link
Contributor

@hzrd149 hzrd149 commented Dec 19, 2024

This PR removes the explicit .js file extension set in vite.config.ts. This was causing issues when using the package with node because node will treat any .js file in a package as an ES module if the "type": "module" is set

This was only an issue when importing the package from a CJS context. for example

const {createRxNostr} = require("rx-nostr")

Would throw

/tmp/tmp.sesywPGnIr/test.cjs:1
const {createRxNostr} = require('rx-nostr')
                        ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/tmp.sesywPGnIr/node_modules/.pnpm/[email protected]/node_modules/rx-nostr/dist/index.cjs.js from /tmp/tmp.sesywPGnIr/test.cjs not supported.
index.cjs.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename index.cjs.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /tmp/tmp.sesywPGnIr/node_modules/.pnpm/[email protected]/node_modules/rx-nostr/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.<anonymous> (/tmp/tmp.sesywPGnIr/test.cjs:1:25) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.18.1

To fix this I removed the fileName option in the vite.config.ts which makes vite use the default .cjs and .mjs file extensions.

So now its possible to import the package with import or require

import { createRxNostr } from "rx-nostr"

const rxNostr = createRxNostr({})
console.log(rxNostr)
const { createRxNostr } = require('rx-nostr')

const rxNostr = createRxNostr({})
console.log(rxNostr)

@penpenpng penpenpng self-requested a review December 19, 2024 12:20
@penpenpng penpenpng changed the title Fix ESM import error in node fix: ERR_REQUIRE_ESM error under CommonJS module system Dec 19, 2024
Copy link
Owner

@penpenpng penpenpng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! I have one change request (or question).

packages/core/package.json Outdated Show resolved Hide resolved
Copy link
Owner

@penpenpng penpenpng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

@penpenpng penpenpng merged commit ad94c9c into penpenpng:main Dec 19, 2024
2 checks passed
@hzrd149 hzrd149 deleted the fix-dist-extensions branch December 19, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants