Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[@rollup/plugin-commonjs] How-to use with native node modules #1416

Unanswered
raphaelmenges asked this question in Q&A
Discussion options

Hello 👋

I am using Rollup through Vite 4 and have a hard time to bundle native Node.js modules that are (still) using commonjs style. In specifically, i am facing following issues:

  1. Rollup tries to parse required .node files, even though the documentation states only ".cjs" are considered by default.
[commonjs--resolver] Unexpected character '�' (Note that you need plugins to import files that are not JavaScript)
file: /Users/<app>/node_modules/<module>/index.js:1:0
  1. Relative require paths to .node binaries seem to be resolved in relation to the project root, not the module that is requiring them.
Could not dynamically require "<project>\build\addon.node". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.

I assume it might be fixable through choosing the correct options. Does anyone know details or has an example at hand how to handle modules in commonjs style that require .node binaries?

You must be logged in to vote

Replies: 2 comments 2 replies

Comment options

@raphaelmenges Have you managed to sole the issue?

You must be logged in to vote
1 reply
Comment options

I am using this template that handles everything as CJS. One can add ESM dependencies via

ssr: {
 noExternal: ["<module-name>"],
 },

I solved this issue a few month ago and cannot remember 100% what lead me to this solution. Soon, Electron will support ESM natively. Then I need to investigate again.

Comment options

import { createRequire } from "module";
export const nodeRequire = createRequire(import.meta.url);

Please use nodeRequire to require your native module

You must be logged in to vote
1 reply
Comment options

Works using nodeRequire, but I'm not happy xD This should just be an auto-optimization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /