Node Electron Require issues with SQLAnywhere - Error: Could not load modules
Node Electron Require issues with SQLAnywhere - Error: Could not load modules
我是 Electron 的新手,在使用 sqlanywhere 包时遇到问题。
我正在做一个非常基本的测试,sqlanywhere 在尝试加载其驱动程序时立即抛出错误。请注意,在我将 Electron 引入此应用程序之前,这一切正常。
这是我的示例代码:
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
}));
});
抛出的错误是:
"Uncaught Exception: Error: Could not load modules for Platform: 'win32', Process Arch: 'x64', and Version: 'v7.9.0"
在我看来,Electron 处理 sqlanywhere 包中的 'require' 语句的方式导致了问题。 sqlanywhere的index.js是:
// ***************************************************************************
// 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;
我添加了上面的两个 console.log 语句以确认正在执行 catch 块,此时 db 仍然为空,而此时它应该已经加载了 x64 驱动程序。同样,这在涉及 Electron 之前一直有效。
Electron 似乎有问题
db = require( "./../bin64/" + driver_file );
如果有人能提供任何见解,我将永远感激不已!
谢谢
我假设您尝试加载的模块包含本机模块。本机模块需要 node.js 进程与编译的二进制文件之间的版本匹配,这意味着如果 node.js 版本在您的节点安装与 Electron 之间不匹配,模块将无法加载。您可以匹配这些特定版本,或使用 https://github.com/electron/electron-rebuild 生成正确的二进制文件。
我是 Electron 的新手,在使用 sqlanywhere 包时遇到问题。
我正在做一个非常基本的测试,sqlanywhere 在尝试加载其驱动程序时立即抛出错误。请注意,在我将 Electron 引入此应用程序之前,这一切正常。
这是我的示例代码:
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
}));
});
抛出的错误是:
"Uncaught Exception: Error: Could not load modules for Platform: 'win32', Process Arch: 'x64', and Version: 'v7.9.0"
在我看来,Electron 处理 sqlanywhere 包中的 'require' 语句的方式导致了问题。 sqlanywhere的index.js是:
// ***************************************************************************
// 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;
我添加了上面的两个 console.log 语句以确认正在执行 catch 块,此时 db 仍然为空,而此时它应该已经加载了 x64 驱动程序。同样,这在涉及 Electron 之前一直有效。
Electron 似乎有问题
db = require( "./../bin64/" + driver_file );
如果有人能提供任何见解,我将永远感激不已!
谢谢
我假设您尝试加载的模块包含本机模块。本机模块需要 node.js 进程与编译的二进制文件之间的版本匹配,这意味着如果 node.js 版本在您的节点安装与 Electron 之间不匹配,模块将无法加载。您可以匹配这些特定版本,或使用 https://github.com/electron/electron-rebuild 生成正确的二进制文件。