-
-
Notifications
You must be signed in to change notification settings - Fork 613
[@rollup/plugin-commonjs] How-to use with native node modules #1416
-
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:
- Rollup tries to parse required
.nodefiles, 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
- Relative require paths to
.nodebinaries 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?
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 5
Replies: 2 comments 2 replies
-
@raphaelmenges Have you managed to sole the issue?
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
import { createRequire } from "module"; export const nodeRequire = createRequire(import.meta.url);
Please use nodeRequire to require your native module
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2 -
😄 2
-
Works using nodeRequire, but I'm not happy xD This should just be an auto-optimization.
Beta Was this translation helpful? Give feedback.