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

upgrade to 3.0.0, ES module error #42

Closed
zhushenwudi opened this issue Oct 2, 2022 · 15 comments
Closed

upgrade to 3.0.0, ES module error #42

zhushenwudi opened this issue Oct 2, 2022 · 15 comments

Comments

@zhushenwudi
Copy link

Hi, again! The code is written in the same way as 2.7, but an error will be reported

import {FileDecoder} from "flac-bindings";
let decoder = new FileDecoder({
    file: srcPath,
})

App threw an error during load
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/flac-bindings/lib/index.js from /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts not supported.
Instead change the require of index.js in /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts to a dynamic import() which is available in all CommonJS modules.
at Object.require.extensions. [as .js] (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/ts-node/dist/index.js:851:20)
at Function.c._load (node:electron/js2c/asar_bundle:5:13331)
at Object. (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts:35:25)
at Module.m._compile (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/ts-node/dist/index.js:857:29)
at Object.require.extensions. [as .ts] (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/ts-node/dist/index.js:859:16)
at Function.c._load (node:electron/js2c/asar_bundle:5:13331)
at Object. (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/windows/desktopLyricWindow.ts:3:16)
at Module.m._compile (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/ts-node/dist/index.js:857:29)
at Object.require.extensions. [as .ts] (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/ts-node/dist/index.js:859:16)
at Function.c._load (node:electron/js2c/asar_bundle:5:13331)
at Object. (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/main.ts:35:46)
at Module.m._compile (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/ts-node/dist/index.js:857:29)
at Object.require.extensions. [as .ts] (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/ts-node/dist/index.js:859:16)
at Function.c._load (node:electron/js2c/asar_bundle:5:13331)
at loadApplicationPackage (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:110:16)
at Object. (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:222:9)
at Function.c._load (node:electron/js2c/asar_bundle:5:13331)
at Object. (node:electron/js2c/browser_init:193:3197)
at Object../lib/browser/init.ts (node:electron/js2c/browser_init:193:3401)
at webpack_require (node:electron/js2c/browser_init:1:128)
at node:electron/js2c/browser_init:1:1200
at node:electron/js2c/browser_init:1:1267
at Function.c._load (node:electron/js2c/asar_bundle:5:13331)

@melchor629
Copy link
Owner

melchor629 commented Oct 2, 2022

Hi again! The error you are facing is totally expected. That's one of the breaking changes of version 3.0.

See https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c which has a good FAQ about ES Modules, and also could be interesting this from node docs.

State of ESM imports in Electron has a dedicated issue in their repo.

@zhushenwudi
Copy link
Author

zhushenwudi commented Oct 10, 2022

The current situation is that Electron does not support this import mode, and the official has not provided a solution, is that so?

@melchor629
Copy link
Owner

Yep, sadly that's more or less the current state. The good thing is node already provides a workaround for this, but depends on the tools you use. import('package') can be used anywhere to load any kind of package (including ESM). The bad part is that TypeScript or bundlers like webpack will rewrite the import() to anything else. I think there is some package around to workaround this. Take a look at the first link I sent you, it gives some tips to face this issue.

A quick expample to convert normal import with dynamic import would be:

// On the top of the file
import { StreamDecoder } from 'flac-bindings';

// Wherever you want, but it should support await
const { StreamDecoder } = await import('flac-bindings');

Sorry for the inconvenience, I had this planned a long time ago already... :(

@zhushenwudi
Copy link
Author

ok,i will try later

@zhushenwudi
Copy link
Author

upgrade to electron@21.1.1, because I saw this merge info:
electron/electron#35930
I use "const { FileDecoder } = await import('flac-bindings');"
it's not work

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/flac-bindings/lib/index.js from /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts not supported.
Instead change the require of index.js in /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts to a dynamic import() which is available in all CommonJS modules.
at require.extensions. [as .js] (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/ts-node/dist/index.js:851:20)
at c._load (node:electron/js2c/asar_bundle:5:13339)
at /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts:234:81
at async /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts:234:33
(node:35675) UnhandledPromiseRejectionWarning: Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/flac-bindings/lib/index.js from /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts not supported.
Instead change the require of index.js in /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts to a dynamic import() which is available in all CommonJS modules.
at require.extensions. [as .js] (/Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/ts-node/dist/index.js:851:20)
at c._load (node:electron/js2c/asar_bundle:5:13339)
at /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts:234:81
at async /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts:234:33
(node:35675) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

@melchor629
Copy link
Owner

Them, as I mentioned before, the import() is getting rewritten (probably thanks to typescript). Read this https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#how-can-i-import-esm-in-electron , hope it helps. I have used it sometime ago and worked fine.

@zhushenwudi
Copy link
Author

zhushenwudi commented Oct 14, 2022

I switch the module of ts-node as esm, and use electron@20.3.2, because when i use electron@21.x.y, official use v8 memory cages and limit 4g heap memory, maybe glasstron-clarity native-module is not compatible.

I also use these code

const {FileDecoder} = await import("flac-bindings")
let decoder = new FileDecoder({
    file: srcPath,
})

But there are still some internal errors in flac binding.

TypeError: flac.DecoderBuilder is not a constructor
    at new FileDecoder (file:///Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/flac-bindings/lib/decoder/file-decoder.js:10:21)
    at /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts:218:23
(node:80726) UnhandledPromiseRejectionWarning: TypeError: flac.DecoderBuilder is not a constructor
    at new FileDecoder (file:///Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/node_modules/flac-bindings/lib/decoder/file-decoder.js:10:21)
    at /Users/hoshizora-rin/Documents/important/electron/LoveLiveMusicPlayer/src/main/util.ts:218:23
(node:80726) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)

perhaps flac.DecoderBuilder in file-decoder.js is not a constructor

@melchor629
Copy link
Owner

Well, now at least it imports the package, that is some good news.

The error now it might be because the native code (the .node file) has not been updated properly. That class de error references is new and comes from that native code.

I might have an issue in the downloader. Try to run a npm rebuild and see if it fixes the issue. Or reinstall the package.

@zhushenwudi
Copy link
Author

Cool! I reinstalled the package and it works!!

@zhushenwudi
Copy link
Author

However, the song previously mentioned cannot be played directly when it is converted by version 3.0.0, which is not only a problem of half frame loss

@zhushenwudi
Copy link
Author

However, the song previously mentioned cannot be played directly when it is converted by version 3.0.0, which is not only a problem of half frame loss

roll back to v2.7.2 is also not work. It's strange. Let me think about it again

@zhushenwudi
Copy link
Author

It's okay now, because my business logic has bugs

@zhushenwudi
Copy link
Author

In addition, I want to know what is the difference between 2 and 3, and what are the optimizations or bug fixes in addition to the conversion bit esm

@melchor629
Copy link
Owner

Oh good to now that 3.0 seems to work fine now :)

Regarding changes from 2.7 to 3.0, there are some good breaking changes:

  • update to libFLAC 1.4.1 (from 1.3.something)
  • some refactors in the native code for people that use the native API directly (you faced with the flac.DecoderBuilder which is a new class in native code)
  • removed support for node 10 and 12 mainly for not being supported anymore
  • officially adding Windows x86 builds
  • the script to download the native code is not a dependency anymore, I maintain a specific version for my use case (one dependency less to install)

Big changes come from the update of libFLAC (see their release notes).

@zhushenwudi
Copy link
Author

thx, I saw that the discussion result was not solved for the time being, then there is no way.
3.0.0 still has half a second empty frame. Let me close this issue temporarily

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

No branches or pull requests

2 participants