您的位置:首页 > Web前端 > Node.js

node-bindings无法在Electron中使用的解决办法

2017-01-08 11:27 399 查看
node-bindings非常好用,但是在Electron中无法使用,我查了一下,是因为fileName以file://开头导致无法定位动态库的根目录。已经提交给作者了,可以临时修改一下node_modules/bindings/bindings.js。

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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  node bindings electron