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 does not work in electron #29

Closed
xianjimli opened this issue Jan 8, 2017 · 6 comments
Closed

getFileName does not work in electron #29

xianjimli opened this issue Jan 8, 2017 · 6 comments

Comments

@xianjimli
Copy link

@xianjimli xianjimli commented Jan 8, 2017

getFileName does not work in electron, In Electron, filename starts with "file://", it should be removed, or bind will failed.

exports.getFileName = function getFileName (calling_file) {
  var origPST = Error.prepareStackTrace
    , origSTL = Error.stackTraceLimit
    , dummy = {}
    , fileName

  Error.stackTraceLimit = 10

  Error.prepareStackTrace = function (e, st) {
    for (var i=0, l=st.length; i<l; i++) {
      fileName = st[i].getFileName()
      if (fileName !== __filename) {
        if (calling_file) {
            if (fileName !== calling_file) {
              return
            }
        } else {
          return
        }
      }
    }
  }

  // run the 'prepareStackTrace' function above
  Error.captureStackTrace(dummy)
  dummy.stack

 // cleanup
  Error.prepareStackTrace = origPST
  Error.stackTraceLimit = origSTL

  //In Electron, filename starts with "file://"
  var fileSchema = "file://";
  if(fileName.indexOf(fileSchema) === 0) {
     fileName = fileName.substr(fileSchema.length);
     //on windows
     if(fileName.indexOf(":/") == 2){
         fileName = fileName.substr(1);
     }
  }

  return fileName
}
@perymimon
Copy link

@perymimon perymimon commented Dec 12, 2018

i still reproduce it when i use leveldown package

@1009466147
Copy link

@1009466147 1009466147 commented Jan 21, 2019

hi I have the same problem,This code returns undefined,In nw.js version 0.14.7 , use ffi
Error.prepareStackTrace = function (e, st) { for (var i=0, l=st.length; i<l; i++) { fileName = st[i].getFileName() if (fileName !== __filename) { if (calling_file) { if (fileName !== calling_file) { return } } else { return } } } }

@E3V3A
Copy link

@E3V3A E3V3A commented Jan 23, 2019

It seem that @TooTallNate is too concerned telling people about his height. 🤔 A PR has fixed the issue but has not been merged...

@thisTom
Copy link

@thisTom thisTom commented Aug 27, 2019

hi I have the same problem,This code returns undefined,In nw.js version 0.14.7 , use ffi
Error.prepareStackTrace = function (e, st) { for (var i=0, l=st.length; i<l; i++) { fileName = st[i].getFileName() if (fileName !== __filename) { if (calling_file) { if (fileName !== calling_file) { return } } else { return } } } }

i have the same problem ,when i use electron and ffi

@pjebs
Copy link

@pjebs pjebs commented Apr 16, 2020

This issue is still not fixed for electron -- at least when bindings package is bundled using rollup.

/**
 * Gets the filename of the JavaScript file that invokes this function.
 * Used to help find the root directory of a module.
 * Optionally accepts an filename argument to skip when searching for the invoking filename
 */

When bundled, it's already embedded in the javascript. There is no other file that calls it.

This is the error:

Uncaught Exception:
TypeError: Cannot read property 'indexOf' of undefined
    at Function.getFileName (/xxx/my-app/out/theCucumber.app.x/the-great-app-darwin-x64/the-great-app.app/Contents/Resources/app/src/bundle.js:260:16)
    at bindings (/xxx/my-app/out/theCucumber.app.x/the-great-app-darwin-x64/the-great-app.app/Contents/Resources/app/src/bundle.js:165:48)
    at Object.<anonymous> (/xxx/my-app/out/theCucumber.app.x/the-great-app-darwin-x64/the-great-app.app/Contents/Resources/app/src/bundle.js:1290:16)
    at Module._compile (internal/modules/cjs/loader.js:968:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:986:10)
    at Module.load (internal/modules/cjs/loader.js:816:32)
    at Module._load (internal/modules/cjs/loader.js:728:14)
    at Module._load (electron/js2c/asar.js:717:26)
    at Function.Module._load (electron/js2c/asar.js:717:26)
    at Object.<anonymous> (electron/js2c/browser_init.js:5785:12)

This is the location of error:

  // handle filename that starts with "file://"
  var fileSchema = 'file://';
  if (fileName.indexOf(fileSchema) === 0) {
    fileName = fileUriToPath_1(fileName);
  }
@pjebs
Copy link

@pjebs pjebs commented Apr 16, 2020

Is it not standard practice to bundle your javascript files into 1 in electron?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

6 participants
You can’t perform that action at this time.