JZZ icon indicating copy to clipboard operation
JZZ copied to clipboard

Fails to detect nodeJS environment in the electron main process when using TypeScript and Webpack

Open DeveloperBlue opened this issue 2 years ago • 7 comments

When using jzz (and it's dependency jazz-midi) in an electron main process written in Typescript and transpiled with Webpack, a dynamic require found in JZZ.js does not work and the library fails to detect that it is in a NodeJS environment.

It is similar to the issue found at jazz-soft/jazz-midi#6

A workaround would be to apply the patch mentioned here as well as the following:

_initNode(require('jazz-midi')); // https://github.com/jazz-soft/JZZ/blob/master/javascript/JZZ.js#L575

should be changed to

const require_dynamic = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
_initNode(require_dynamic('jazz-midi'));

I'm not sure if typescript/webpack/electron issues are within the scope of this project, but I thought I would mention the fix I'm using.

DeveloperBlue avatar Nov 10 '22 19:11 DeveloperBlue

Hi! Thank you for the notice! Electron is not a Node.js environment. Did you try https://github.com/jazz-soft/jazz-midi-electron ?

jazz-soft avatar Nov 11 '22 03:11 jazz-soft

Hi! Electron has two process models, the main process (a NodeJS environment) and a renderer process (the browser/chromium environment that you are thinking about). This issue is for the main process, which is a NodeJS environment.

The issue is not really caused by Electron directly, but by Webpack being unable to detect the dynamic require when generating the JavaScript bundles.

Personally, I would not change anything in jzz or jazz-midi because so few people will run into this problem, I recommend just adding a small note in the READMEs for anyone using a JavaScript bundler (Webpack, Gulp, Babel, Browserify, etc.). Even this issue existing is enough, as someone will probably find it while they are troubleshooting.

DeveloperBlue avatar Nov 11 '22 05:11 DeveloperBlue

I made some Webpack fixes. Please check if that resolved your issue...

jazz-soft avatar Jan 07 '23 06:01 jazz-soft

The changes made in https://github.com/jazz-soft/JZZ/commit/dab1c62c04a2a0043bba8037080b425704b8badc did not fix the webpack issue for me.

I had to add the jazz-midi package to the externals defined in the webpack.main.config.ts file:

externals: "jazz-midi"

nullobject avatar Mar 19 '23 23:03 nullobject

Did it work?

jazz-soft avatar Mar 20 '23 02:03 jazz-soft

Did it work?

Only after I made the change to the webpack configuration.

nullobject avatar Mar 20 '23 03:03 nullobject

Is your project available online? I'd like to look what the problem was...

jazz-soft avatar Mar 20 '23 03:03 jazz-soft