I currently have a functioning Electron application that utilizes Typescript. The main.ts file is specifically for the main process.
import { app, BrowserWindow } from 'electron';
import * as path from 'path';
import * as url from 'url';
// import knex from 'knex';
// import config from '../knexconfig';
// const db = knex(config);
let mainWindow: Electron.BrowserWindow | null;
async function init() {
// db.select('*').from('users').then((rows) => {
// console.log(rows);
// });
createWindow();
}
function createWindow() {
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = '1';
if (process.env.NODE_ENV === 'development') {
mainWindow.loadURL(`http://localhost:4000`);
mainWindow.webContents.openDevTools();
} else {
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
})
);
}
mainWindow.on('closed', () => {
mainWindow = null;
});
}
app.on('ready', init);
At the moment, there are 6 lines of code commented out at the top. As it stands, the application works fine with this setup. However, enabling these commented lines triggers 10 errors as follows:
"Module not found. Can't resolve ... in ...\node_modules\knex\lib\dialects..."
Here are the detailed errors:
ERROR in ./node_modules/knex/lib/dialects/better-sqlite3/index.js 7:11-36
Module not found: Error: Can't resolve 'better-sqlite3' in 'C:\Users\timom\WebstormProjects\am-calendar\node_modules\knex\lib\dialects\better-sqlite3'
@ ./node_modules/knex/lib/dialects/index.js 6:26-53
@ ./node_modules/knex/lib/knex-builder/internal/config-resolver.js 5:36-61
@ ./node_modules/knex/lib/knex-builder/Knex.js 7:26-63
@ ./node_modules/knex/lib/index.js 1:13-43
@ ./node_modules/knex/knex.js 8:13-35
@ ./electron/main.ts 8:0-24 10:9-13
<!-- More errors continue -->
webpack 5.89.0 compiled with 10 errors and 2 warnings in 1270 ms
Two errors point to missing postgres and sqlite3, even though mysql is the specified database type in the configuration file. Conversely, one error states that mysql is missing, despite being installed on my system.
Below is the configuration file used to run the Electron app:
const path = require('path');
module.exports = {
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
devtool: 'source-map',
entry: './electron/main.ts',
target: 'electron-main',
module: {
rules: [
{
test: /\.(js|ts|tsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
}
],
},
output: {
path: path.resolve(__dirname, './dist'),
filename: '[name].js',
},
};
And here is the package.json file for reference:
{
"name": "am-calendar",
"version": "1.0.0",
"description": "",
"main": "./dist/main.js",
<!-- Remaining content omitted for brevity -->
}