Reputation: 146
I'm doing a simple newsletter app in electron, I need to connect to my database, but if I use "require" to call mysql library it gives me "require is not defined", I tryed to use browsify but it didn't work, somebody can help me? (This script is in an HTML page)
<script>
document.getElementById("btn").addEventListener("click", () => {
var mysql = require (['./node_modules/mysql']);
var connection = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'password',
database: 'db',
});
$sql = 'SELECT `id`,`first_name`,`last_name`,`email` FROM `subscribers`';
connection.query($sql, function (error, results, fields) {
if (error) throw error;
console.log(results);
$('#resultDiv').text(results[0].email);
});
connection.end();
})
</script>
Upvotes: 0
Views: 84
Reputation: 509
Require is not part of the standard JS API, it is part of the Node API. There is a great writeup here. The renderer process in electron, where the HTML is rendered, does not have access to the node API directly. You can setup a framework like Angular or React which gives you more more robust development environment, or you can use the Node API in the main process to fetch the data for you. You can send information back and forth using IPC calls to allow the HTML page to signal to the main process that information is needed. The docs here do a good job showing how this is done.
I think you'd be more successful however using a web development framework rather than using IPC calls for this purpose.
Upvotes: 1