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
}