I'm making a small application that gets/stores data using sqlite3.
Everything was fine during development stage. But when I used electron-packager to pack my application, sqlite3
doesn't work anymore.
The console display an exception which is : Cannot find module 'sqlite3'.
This is my renderer configuration:
return {
target: 'electron-renderer',
mode: argv.production ? 'production' : 'development',
context: paths.root,
devtool: !argv.production ? 'source-map' : false,
entry: {
'app': path.resolve(paths.app, 'app.js')
},
optimization: {
runtimeChunk: false,
splitChunks: {
chunks: 'all',
cacheGroups: {
default: {
enforce: true,
priority: 1
},
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: 2,
name: 'vendors',
enforce: true,
chunks: 'async'
}
}
}
},
module: {
rules: require('./rule')(paths, argv).get()
},
plugins: require('./plugin')(paths, argv).get(),
output: {
path: paths.dist,
filename: 'app.bundled.js'
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: [".ts", ".tsx", ".js"],
alias: {
// summernote: codemirror
'CodeMirror': 'codemirror',
}
},
watch: !argv.production,
watchOptions: {
poll: false
},
externals: {
sqlite3: 'commonjs sqlite3'
}
};
This is the file belongs to renderer process in which I include sqlite3
:
const typeorm = require('typeorm');
const EntitySchema = typeorm.EntitySchema;
const path = require('path');
const electron = require('electron');
require('sqlite3');
module.exports = (ngModule) => {
ngModule.service('$db', (toastr) => {
//#region Properties
// Instance of database connection
let dbConnection = null;
//#endregion
let out = {
//#region Methods
if (dbConnection == null || forceReinitialize) {
// Build a absolute path to database.
const appPath = electron.remote.app.getAppPath();
const dbPath = path.join(appPath, 'assets/db/PersonalCv.db');
return typeorm
.createConnection({
type: "sqlite",
database: dbPath,
synchronize: false,
entities: [
new EntitySchema(require('../models/entities/user')),
new EntitySchema(require('../models/entities/user-decription')),
new EntitySchema(require('../models/entities/skill-category')),
new EntitySchema(require('../models/entities/skill')),
new EntitySchema(require('../models/entities/personal-skill')),
new EntitySchema(require('../models/entities/project')),
new EntitySchema(require('../models/entities/project-skill')),
new EntitySchema(require('../models/entities/project-responsibility')),
new EntitySchema(require('../models/entities/responsibility'))
]
})
.then((connection) => {
dbConnection = connection;
return dbConnection;
})
.catch((error) => {
toastr.error(error);
throw error;
});
}
return new Promise(resolve => {
resolve(dbConnection);
});
},
/*
* Get repository by using name (table name)
* */
getRepository: (name) => {
return out
.getConnection()
.then((connection) => {
return connection.getRepository(name);
});
}
//#endregion
};
return out;
});
};
Here is my repo in case anyone needs further information.
Can anyone help me please ?
Thank you,
After going through some Q&A of electron-packager
and electron-builder
. I found one solution to make sqlite3
to work with webpack
.
In webpack.config.js
, I added :
externals: {
sqlite3: 'commonjs sqlite3'
}
Instead of electron-packager
, I used electron-builder
to build my application. In build
configuration, I copied sqlite3
module to dist
folder.