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

getFileName() returns undefined when called using eval #50

Open
weshouman opened this issue Sep 30, 2018 · 12 comments
Open

getFileName() returns undefined when called using eval #50

weshouman opened this issue Sep 30, 2018 · 12 comments

Comments

@weshouman
Copy link

What happens:
Bindings fails to get the root dir

Reproduction:
calling bindings through eval
Example: Using webpack and having bindings in a dependency (ie: of zmq)

Analysis:
While getting the file name from the stack trace, getFileName() returns undefined.
As filenames could be stored st[i].getEvalOrigin() not only in st[i].getFileName() based on https://github.com/v8/v8/wiki/Stack-Trace-API

weshouman added a commit to weshouman/Langformy that referenced this issue Sep 30, 2018
ZMQ uses bindings which is incompatible with webpack.
Details: bindings tries to reach the directory structure found in the development directory, which is not the case in the dist directory.
More details: bindings uses the prepareStackTrace to examine the trace files, and webpack calls zmq--(thus)-->bindings.js through eval which doesn't result in having the filenames accessible through getEvalOrigin() instead of getFileName() follow [this issue](TooTallNate/node-bindings#50).
@sumshare
Copy link

but webpack st[i].getEvalOrigin() is webpack:///./node_modules/ref/lib/ref.js,it's not a right dir name

@zygzagZ
Copy link

zygzagZ commented Sep 5, 2019

It should work if you specify module using node-bindings as external in webpack configuration (webpack docs) eg. externals: ['zmq']. In my case (I use webpack-vue) iit turned out webpack.config.js was not used at all, I needed to add externals in vue.config.js.

@johnnysprinkles
Copy link

johnnysprinkles commented Sep 6, 2019

I'm using Node 12 and ffi-napi module, compiled through Webpack, and I'm also seeing this:

[node] webpack:///./node_modules/bindings/bindings.js?:178
[node]   if (fileName.indexOf(fileSchema) === 0) {
[node]                ^
[node]
[node] TypeError: Cannot read property 'indexOf' of undefined
[node]     at Function.getFileName (webpack:///./node_modules/bindings/bindings.js?:178:16)
[node]     at bindings (webpack:///./node_modules/bindings/bindings.js?:82:48)
[node]     at eval (webpack:///./node_modules/ref-napi/lib/ref.js?:7:102)
[node]     at Object../node_modules/ref-napi/lib/ref.js (/local/home/jsimons/workspace/nodetemplate/src/NodeStarterTemplate/dist/server/main.js:3366:1)
[node]     at __webpack_require__ (/local/home/jsimons/workspace/nodetemplate/src/NodeStarterTemplate/dist/server/main.js:20:30)
[node]     at eval (webpack:///./node_modules/ffi-napi/lib/ffi.js?:7:13)
[node]     at Object../node_modules/ffi-napi/lib/ffi.js (/local/home/jsimons/workspace/nodetemplate/src/NodeStarterTemplate/dist/server/main.js:1951:1)
[node]     at __webpack_require__ (/local/home/jsimons/workspace/nodetemplate/src/NodeStarterTemplate/dist/server/main.js:20:30)

Think the project owners would accept a pull request that just initializes the fileName to empty string instead of undefined?

@zygzagZ
Copy link

zygzagZ commented Sep 6, 2019

fileName is changed during the function, just initializing it would not work. This however doesn't help you, because when working with webpack, getting this error means that webpack packed this module, which it should not. You must configure webpack to use this module externally, only then you get the low level bindings to work.

@johnnysprinkles
Copy link

Cool, I was running all my serverside files through Webpack so I can have JSX everywhere and use import everywhere. I made it so only my isomorphic React views go through Webpack, everything else runs raw from source. (Still get import statements with the new --experimental-modules flag.)

Still seems like this module could function without caring what file the code is in, but I don't know much about the module. Things are working so I'm happy.

@zygzagZ
Copy link

zygzagZ commented Sep 10, 2019

Yo could have still used webpack for all of the source just by configuring it to use ffi-napi externally

@warpdesign
Copy link

warpdesign commented Nov 4, 2019

I am having the same exception, trying to package an Electron app that has a native dependency.

I tried adding the module in the externals array of the webpack configuration but then it crashes because it cannot find the module:

function(e, t) {
e.exports = drivelist
}

Uncaught ReferenceError: drivelist is not defined

That's how I'm importing drivelist:

import * as drivelist from 'drivelist';

Note that I tried to use require instead of import and it leads to the same error.

drivelist is the module I have added to externals. So it did not include it in the webpack build but is expecting me to load it it seems. I'm not sure how to do that.

@ntbosscher
Copy link

I've been using this non-idea workaround for my native modules in electron and it works for me:

  1. Create <project-root>/build folder for my *.node files
  2. Add build/*.node folder to my electron-builder config (in package.json)
  3. Symbolically link the *.node files from node_modules (e.g. mklink build/better_sqlite3.node node_modules/better-sqlite3/build/Release/better_sqlite3.node)
  4. Monkey patch TooTallNate/node-bindings in my main js file
const bindings = require("bindings");

bindings.getFileName = function getFileName(calling_file) {
    //..
   if(!fileName) return "";
   //..
}

bindings.getRoot = function getRoot(file) {
    return require("electron").remote.app.getAppPath();
}

@ntbosscher
Copy link

#61 <- this might be a better solution (haven't tested)

ntbosscher added a commit to ntbosscher/node-bindings that referenced this issue May 25, 2022
ntbosscher added a commit to ntbosscher/node-bindings that referenced this issue May 25, 2022
@AnEmortalKid
Copy link

AnEmortalKid commented Jul 9, 2022

As a work around, I ended up using webpack for a specific binding that was missing (in electron) and just copying it into the build/Release

    new CopyPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, 'node_modules', 'forcefocus', 'bin', 'win32-x64-106'),
          to: path.resolve(__dirname, 'build', 'Release')
        }
      ]
    })

Since I was using electron-forge for my problem, I was also able to get arround it by using electron-forge-externals

Would be nice to have #74

@FrederickThunder
Copy link

@ntbosscher @AnEmortalKid This thread is quite old but just want to ask if you had the same issue when running make (electron-forge make) or even with just electron-forge start?
I'm facing issue after packaging and installing .deb in linux machine although electron-forge start works just fine.

@AnEmortalKid
Copy link

@FrederickThunder it was probably with make, given that I was using the build/Release folder as a copied pattern.

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

8 participants