TooTallNate/node-bindings

getFileName cannot read property of undefined

tzarebczan opened this issue ยท 18 comments

Seeing the below when using https://github.com/jostrander/mouse-forward-back in our project (lbryio/lbry-desktop@cd8436c). We are using webpack 3.

      at Function.getFileName (webpack-internal:///./node_modules/bindings/bindings.js:176:16)
      at bindings (webpack-internal:///./node_modules/bindings/bindings.js:82:48)
      at eval (webpack-internal:///./node_modules/mouse-forward-back/mouse-forward-back.js:3:82)
      at Object../node_modules/mouse-forward-back/mouse-forward-back.js (/home/tom/lbry-desktop/dist/main/main.js:739:1)
      at __webpack_require__ (/home/tom/lbry-desktop/dist/main/main.js:655:30)
      at fn (/home/tom/lbry-desktop/dist/main/main.js:65:20)
      at __webpack_exports__.a.appState (webpack-internal:///./src/main/createWindow.js:18:24)
      at App.eval (webpack-internal:///./src/main/index.js:120:91)
      at Generator.next (<anonymous>)
      at step (webpack-internal:///./src/main/index.js:24:191)

What is webpack-internal://?

I'm also getting the issue.

It looks like

Error.captureStackTrace(dummy) is setting dummy to have an undefined stack member.

https://github.com/TooTallNate/node-bindings/blob/master/bindings.js#L171

I added some debug logic:

    console.debug('getFileName|debug|3', {
      dummy,
      'dummy.stack': dummy.stack,
    })
getFileName|debug|3 { dummy: {}, 'dummy.stack': undefined }

same here. I was using node-speaker in a React project with Webpack.
In webpack.config.js I set node: { fs: 'empty', console: true, net: 'empty', tls: 'empty' }, it shows the following error:

Uncaught TypeError: Cannot read property 'indexOf' of undefined
    at Function.getFileName (bindings.js:180)
    at bindings (bindings.js:84)
    at eval (index.js:9)
    at Object../node_modules/speaker/index.js (Panel.bundle.js:7426)
    at __webpack_require__ (Panel.bundle.js:724)
    at fn (Panel.bundle.js:101)
    at Object.eval (AWSPolly.js:11)
    at eval (AWSPolly.js:50)
    at Object../src/components/App/AWSPolly.js (Panel.bundle.js:7756)
    at __webpack_require__ (Panel.bundle.js:724)

A follow-up question: can node-speaker be used in a front-end project?

If I set target: "node" in webpack.config.js, it would show Uncaught ReferenceError: require is not defined.
The reason is bindings.js has var fs=require("fs"). In Webpack, by setting target:"node", it will not pack fs and require. But these two do not work for browsers.
On the other hand, if set target:"web", it would show fs module not found.

I tried many ways but had not solve it yet.
I hope I made myself clear.

I hit the same issue today. Using for electron. Is there any workaround other than changing target or removing fs, because, i need those two

For what it's worth, I've also hit this issue when trying to include a local native node module that uses bindings in Electron (no webpack involved here). Haven't figured out a workaround yet.

For what it's worth, I've also hit this issue when trying to include a local native node module that uses bindings in Electron (no webpack involved here). Haven't figured out a workaround yet.

exact the same problem here.

Same error :/

Facing same issue, has anyone come up with a solution or workaround? Thanks!

I'm also encountering this error in a project that uses better-sqlite3 (depends on this).

Same issue here as @ajmar with electron and better-sqlite3 as well.

I've encountered this issue using Rollup. It seems to be caused by rollup deleting this line:

dummy.stack;

That dummy.stack; line is what actually causes the prepareStackTrace() to run. This seems to be some mysterious lazy-evaluation insanity? I guess some optimisation pass somewhere in the bundler makes the not-unreasonable decision that this line can't have side effects and eats it.

If you change the line to dummy = dummy.stack; then things start working as expected. I suggest refactoring this whole function such that its dataflow no longer relies on mysticism and goat sacrifice.

pjebs commented

I've got the same issue with rollup (bundler) and an electron app. A recent PR supposedly made it work with electron, but I don't think it factored in bundling.

See: #29 (comment)

@etylermoss @Stefano1990 @TooTallNate See #66. I proposed a fix for this problem, which does resolve the issues I have.

Had the same issue with drivelist in an electron app using webpack. Worked fine in dev but not built for production. Finally found some hints on similar issues and found that adding "drivelist" to externals in webpack config solved the issue for us. Hope it helps

Problem is that if your optimizer is too aggressive, this call will get removed:

dummy.stack;
So the stack getter is not called which causes the error mentioned above.

Changing the optimizer settings not to optimize such code or exclude drivelist by setting it as external as mentioned above will also work.

I fixed it by changing the terser options like this in webpack configuration file;

optimization: {
        minimizer: [
            new TerserPlugin({
                cache: true,
                parallel: true,
                terserOptions: {
                    compress: {
                        reduce_vars: false
                    }
                }
              }),
        ]
    },

This s a huge issue. Took me hours to figure out a workaround...

Was no fix found for this?> I'm experiencing the issue with node-midi

I find a solution for my Electron + Vue app

pluginOptions: {
    electronBuilder: {
      externals: ['speaker'], // add this line
      ...