Closed zhushenwudi closed 1 year ago
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.
The current situation is that Electron does not support this import mode, and the official has not provided a solution, is that so?
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... :(
ok,i will try later
upgrade to electron@21.1.1, because I saw this merge info: https://github.com/electron/electron/pull/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.--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
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.
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
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.
Cool! I reinstalled the package and it works!!
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
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
It's okay now, because my business logic has bugs
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
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:
libFLAC
1.4.1 (from 1.3.something)flac.DecoderBuilder
which is a new class in native code)Big changes come from the update of libFLAC
(see their release notes).
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
Hi, again! The code is written in the same way as 2.7, but an error will be reported
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)