I'm new to Electron and am having issues using the sqlanywhere package with it.
I'm doing a very basic test, and sqlanywhere is throwing an error immediately when it is trying to load its drivers. Note that this works fine until I involve Electron in this application.
Here is my sample code:
const sqlanywhere = require('sqlanywhere');
const url = require('url');
const path = require('path');
const { app, BrowserWindow, Menu, Tray, ipcMain } = require('electron');
let conn = sqlanywhere.createConnection();
var conn_params = {
Host : 'localhost:2638',
UserId : 'user',
Password: 'password',
ConnectionPool: 'YES(MaxCached=10)'
};
conn.connect(conn_params, function(err) {
if (err) throw err;
conn.exec('SELECT * from mytable', function (err, result) {
if (err) throw err;
console.log(result[0]);
conn.disconnect();
})
});
let mainWindow;
app.on('ready', () => {
console.log("Started...");
// Create Window
mainWindow = new BrowserWindow({
width: 200,
height: 200
});
// Load HTML file into Window
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'mainWindow.html'),
protocol: 'file:',
slashes: true
}));
});
The error thrown is:
"Uncaught Exception: Error: Could not load modules for Platform: 'win32', Process Arch: 'x64', and Version: 'v7.9.0"
It seems to me that the way Electron is handling the 'require' statements in the sqlanywhere package is causing the problem. The index.js of sqlanywhere is:
// ***************************************************************************
// Copyright (c) 2017 SAP SE or an SAP affiliate company. All rights reserved.
// ***************************************************************************
var db = null;
var driver_file = "sqlanywhere"
var v = process.version;
var match = v.match( 'v([0-9]+)\.([0-9]+)\.[0-9]+' );
driver_file += '_v' + match[1];
if( match[1]+0 == 0 ) {
driver_file += '_' + match[2];
}
try {
if( process.arch == "x64" ) {
db = require( "./../bin64/" + driver_file );
} else if( process.arch == "ia32" ) {
db = require( "./../bin32/" + driver_file );
} else {
throw new Error( "Platform Not Supported" );
}
} catch( err ) {
try {
// Try finding natively compiled binaries
console.log("Error thrown"); // added by me
console.log("DB: " + db); // db is null
db = require( "./../build/Release/sqlanywhere.node" );
} catch( err ) {
throw new Error( "Could not load modules for Platform: '" +
process.platform + "', Process Arch: '" + process.arch +
"', and Version: '" + process.version +"'" );
}
}
module.exports = db;
I've added the two console.log statements above to confirm the catch block is being executed, and that db is still null at this point, when it should have loaded the x64 driver. Again, this works until Electron is involved.
It seems Electron may be having issues with
db = require( "./../bin64/" + driver_file );
If anyone could offer any insight I would be forever appreciative!
Thanks
I assume module you are try to load contains native module. Native module requires version matching between node.js process to compiled binaries, meaning if node.js version does not match between your node installation to Electron, module won't be able to loaded. You could match those specific version between, or use https://github.com/electron/electron-rebuild to generate correct binaries.