ElectronJs: please install sqlite3 package manually
I solved this by using dialectModule
option in sequelize
.
// db.ts
import sqlite3 from "sqlite3"
import { Sequelize } from "sequelize"
const sequelize = new Sequelize( {
dialect: "sqlite",
dialectModule: sqlite3, // Set it here.
storage: ":memory:",
} )
If you are getting the error cannot find node module sqlite3 or please install sqlite3 package manually in electron
application after install sqlite
. Then this solution can be help full to fix your issue.
Step 1: We need to clone electron quick start project on your local
Source: https://electronjs.org/docs/tutorial/first-app
$ git clone https://github.com/electron/electron-quick-start
Step 2: Go into the repository
$ cd electron-quick-start
Step 3: Install electronJs dependencies
$ npm install
Step 4: Run electron project
$ npm start
Now lets come to most challenging part
Step 5: Now we need to install sqlite dependency
$ npm install sqlite3 --save
$ npm install sequelize --save
Step 6: Open main.js in your IDE and add this code at the end of file to create database connection
Source: http://docs.sequelizejs.com/manual/installation/usage.html
// Create database connection
const Sequelize = require('sequelize');
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
storage: './database.sqlite'
});
// Test connection
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
Step 7: Now again start your electron project using npm start
and you will start getting exception
Error: Please install sqlite3 package manually
How we can fix it?
Step 8: To fix this error we need to install electron-rebuild package. Make sure you are using cmd run as Administrator before execute this command otherwise it will never work.
$ npm install --save-dev electron-rebuild
Step 9: Now we have to configure the rebuild command in package.json
under the script
"scripts": {
"rebuild": "electron-rebuild -f -w sqlite3"
}
Step 10: Now we Need to install python and set it into Path in environment variable.
- Download Python: https://www.python.org/download/releases/2.7/
- Open Control Panel > System and Security > System > Advanced System Setting > Enviroment Variables
- Under User variables for %username% section click on new to add a new Item
- Set variable name
Path
and variable valueC:\Python27
(where you have installed python) and click on OK Button
Step 10: Now open a new command line and rebuild your electron project
$ npm run rebuild
Step 11: And now you can run it and it will works fine
$ npm start
Happy Programming.
NOTE: If you will get this error on Step 10 while running npm run rebuild
× Rebuild Failed
An unhandled error occurred inside electron-rebuild
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framework 2.0 SDK, 2) install Microsoft Visual Studio 2005 or 3) add the location of the component to the system path if it is installed elsewhere. [C:\Projects\Test\node_modules\sqlite3\build\binding.sln]
gyp ERR! build error
gyp ERR! stack Error: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (C:\Projects\Test\node_modules\node-gyp\lib\build.js:262:23)
gyp ERR! stack at emitTwo (events.js:106:13)
gyp ERR! stack at ChildProcess.emit (events.js:191:7)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
gyp ERR! System Windows_NT 10.0.17134
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Projects\\Test\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--target=1.8.8" "--arch=x64" "--dist-url=https://atom.io/download/electron" "--build-from-source" "--module_name=node_sqlite3" "--module_path=C:\\Projects\\Test\\node_modules\\sqlite3\\lib\\binding\\electron-v1.8-win32-x64" "--host=https://mapbox-node-binary.s3.amazonaws.com" "--remote_path=./{name}/v4.0.2/{toolset}/" "--package_name=electron-v1.8-win32-x64.tar.gz"
gyp ERR! cwd C:\\Projects\\Test\node_modules\sqlite3
gyp ERR! node -v v6.9.5
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Failed with exit code: 1
Error: Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framework 2.0 SDK, 2) install Microsoft Visual Studio 2005 or 3) add the location of the component to the system path if it is installed elsewhere. [C:\Projects\Test\node_modules\sqlite3\build\binding.sln]
gyp ERR! build error
gyp ERR! stack Error: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (C:\Projects\Test\node_modules\node-gyp\lib\build.js:262:23)
gyp ERR! stack at emitTwo (events.js:106:13)
gyp ERR! stack at ChildProcess.emit (events.js:191:7)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
gyp ERR! System Windows_NT 10.0.17134
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Projects\\Test\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--target=1.8.8" "--arch=x64" "--dist-url=https://atom.io/download/electron" "--build-from-source" "--module_name=node_sqlite3" "--module_path=C:\\Projects\\Test\\node_modules\\sqlite3\\lib\\binding\\electron-v1.8-win32-x64" "--host=https://mapbox-node-binary.s3.amazonaws.com" "--remote_path=./{name}/v4.0.2/{toolset}/" "--package_name=electron-v1.8-win32-x64.tar.gz"
gyp ERR! cwd C:\\Projects\\Test\node_modules\sqlite3
gyp ERR! node -v v6.9.5
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Failed with exit code: 1
at SafeSubscriber._error (C:\Projects\Test\node_modules\spawn-rx\lib\src\index.js:277:84)
at SafeSubscriber.__tryOrUnsub (C:\Projects\Test\node_modules\rxjs\Subscriber.js:242:16)
at SafeSubscriber.error (C:\Projects\Test\node_modules\rxjs\Subscriber.js:201:26)
at Subscriber._error (C:\Projects\Test\node_modules\rxjs\Subscriber.js:132:26)
at Subscriber.error (C:\Projects\Test\node_modules\rxjs\Subscriber.js:106:18)
at MapSubscriber.Subscriber._error (C:\Projects\Test\node_modules\rxjs\Subscriber.js:132:26)
at MapSubscriber.Subscriber.error (C:\Projects\Test\node_modules\rxjs\Subscriber.js:106:18)
at SafeSubscriber._next (C:\Projects\Test\node_modules\spawn-rx\lib\src\index.js:251:65)
at SafeSubscriber.__tryOrSetError (C:\Projects\Test\node_modules\rxjs\Subscriber.js:251:16)
at SafeSubscriber.next (C:\Projects\Test\node_modules\rxjs\Subscriber.js:191:27)
Then execute this in command line npm install windows-build-tools --global
And while installing windows-build-tools
if you will get this exception
async function aquireInstallers(cb) {
^^^^^^^^
SyntaxError: Unexpected token function
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\Users\User\AppData\Roaming\npm\node_modules\windows-build-tools\dist\start.js:4:29)
It means you are using old version of nodeJS to fix it you can install old version of windows-build-tools using npm install [email protected] --global
And now you can start the project using npm start
If you use sqlite3 via sequelize, you can avoid the error with adding sequelize to externals of webpack.config like this.
module.exports = {
// other configs ..
externals: {
"sequelize": "require('sequelize')",
},
}
Reference: https://www.bountysource.com/issues/38723672-can-not-pack-sequelize-with-sqlite3