melchor629 / node-flac-bindings

Nodejs bindings to libFLAC
ISC License
17 stars 1 forks source link

New release breaks my code #43

Closed DrCWO closed 9 months ago

DrCWO commented 1 year ago

I currently use "flac-bindings": "^3.0.0", with node v19.0.0

In the code I ran on node v16 I used "flac-bindings": "^2.7.0" and ran fine.

Here my code that runs fine with v2.7.0 on node v16:

var flacBindings = require('flac-bindings');
StreamEncoder = flacBindings.StreamEncoder;

Running with v3.0.0 on node v19 I get:

var flacBindings = require('flac-bindings');
                     ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/pi/rooExtend/node_modules/flac-bindings/lib/index.js from /home/pi/rooExtend/modPlay.js not supported.
Instead change the require of index.js in /home/pi/rooExtend/modPlay.js to a dynamic import() which is available in all CommonJS modules.

Changing my code as recommended to import { StreamEncoder } from 'flac-bindings';

I see this error:

/home/pi/rooExtend/modPlay.js:111
                import { StreamEncoder } from 'flac-bindings';
                ^^^^^^

SyntaxError: Cannot use import statement outside a module

Help would be appreciated as I have to finish my new release and am stuck at that point.

Thanks' DrCWO

DrCWO commented 1 year ago

Next I tried to dynamically import so I did: var flacBindings = require('flac-bindings'); instead of var flacBindings = require('flac-bindings');

But then I get an error in the next line if I like to create an instance of StreamEncoder:

/home/pi/rooExtend/modPlay.js:928
                var encoder = new StreamEncoder({
                              ^

TypeError: StreamEncoder is not a constructor
DrCWO commented 1 year ago

OK, for all with the same issue I will publish the solution 👍

This was the code before:

var flacBindings = require('flac-bindings');
StreamEncoder = flacBindings.StreamEncoder;
// ... your code continues here...

This is the code that you have to use now. It dynamically loads the module and this takes some time. Therefore you have to wait and continue asynchronously...

        import('flac-bindings')
            .then(flacBindings => {
                StreamEncoder = flacBindings.StreamEncoder;
                // ... after loading the flac-bindings your code continues here...
            })
            .catch(err => {
                console.log('Cannot load flac-bindings ' + err);
            })

Hope this is useful for someone else but me.

@melchor629 will be great if you might add this to your README file

Best DrCWO

melchor629 commented 1 year ago

Hi DrCWO! Thanks for opening the issue.

This is a known "pain point" of the 3.x series. In release notes I pointed it out that this was happening and added this link https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c which explains a bit what happens and what you can do to fix it. You can always keep using 2.x series meanwhile or use the dynamic import() syntax.

In addition, because I imagined that this would be a recurrent issue, the README also contains this line to warn the devs. I see I should make it even clearer.

Sorry for this confusion. I wanted to move forward with the technology and I know that this can be a bit frustrating. I hope you could find the above link useful.

kmturley commented 11 months ago

I had the same issue. might be worth adding to the comment to use v2.x if you need CommonJS support? https://github.com/melchor629/node-flac-bindings/tree/v2.7.2

melchor629 commented 11 months ago

Added a note for it in the readme just in case, thanks

melchor629 commented 9 months ago

added a custom bundle with cjs under require('flac-bindings') in latest version

zhushenwudi commented 9 months ago

So can we now upgrade v2 to v3 without any issues? @melchor629

melchor629 commented 9 months ago

Give it a try and let's hope nothing breaks. In my really limited testing, everything seem to work fine, but it may have any unwanted surprise. Raise an issue if you find something not working.