cancel
Showing results for 
Search instead for 
Did you mean: 

Nodejs sqlanywhere Electron - Error: Could not load modules

2,330

Hi

I'm creating a nodejs app and making use of the sqlanywhere module. Everything works great until I involve "Electron" to build a UI for this app. Is Electron supported by the sqlanywhere package?

I'm doing a very basic test, and sqlanywhere is throwing an error immediately when it is trying to load its drivers.

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 module is causing the problem. The value returned by db below is NULL whenever I include Electron in my app

    // ***************************************************************************
    // 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 );
        console.log("DIR: " + __dirname);
        db = require( "/node_modules/sqlanywhere/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");
        console.log("DB: " + db);
        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;

Any help would be GREATLY appreciated!

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

graeme_perrow
Advisor
Advisor

It looks like external modules need to be rebuilt in order to be used with electron, and the error you're seeing is due to the fact that the sqlanywhere module wasn't rebuilt. We install pre-built binaries on Windows so we can't rebuild it in place. I'll have to do some more investigation and get back to you.

0 Kudos

Thanks this is exactly what it was - I was able to use electron-rebuild to rebuild the module.

The current version of node is 9.2 - sqlanywhere currently caters for drivers up to node 8.x. Do you know if there is a v9 driver in the works?

Many thanks

graeme_perrow
Advisor
Advisor
0 Kudos

Excellent, I didn't think electron-rebuild would work but I'm glad it did. A v9 driver is in the works but I don't have an ETA.

Answers (0)