RavenMan
RavenMan

Reputation: 1923

Packaged Electron App cannot find module sqlite3

Background

Within an Electron app, sqlite3 doesn't work right after npm install --save. I got it working after doing some web research, by:

(1) downgrading my node version (via n) to match Electron's node version; and

(2) rebuilding sqlite with the right module name/targets with this package.json script: "rebuild-sqlite3": "cd node_modules/sqlite3 && npm run prepublish && node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.2-darwin-x64 && node-gyp rebuild --target=1.2.6 --arch=x64 --target_platform=darwin --dist-url=https://atom.io/download/atom-shell --module_name=node_sqlite3 --module_path=../lib/binding/electron-v1.2-darwin-x64"

After that, Sqlite3 works in development (i.e. when I run electron . in the project dir).

Problem

I package the app with electron-packager: "./node_modules/.bin/webpack -p && ./node_modules/electron-packager/cli.js ./ --out ./bin --platform=darwin --arch=x64 --version=1.2.6 --overwrite --ignore=\"ignore|bin|node_modules\""

When I open the app by double-clicking the created (appName).app, the console complains: `Uncaught Error: Cannot find module

Question

How can I get sqlite3 to be found in the final packaged product? Even "hacks" or workarounds are welcome, as I tried various fixes to no avail.

Upvotes: 3

Views: 3761

Answers (2)

Jayground
Jayground

Reputation: 1877

I leave this answer for someone who has the same problem and is wasting their time to figure out.

step1

npm install sqlite3

electron .

I expected to work fine. but I had an error 'cannot find node module sqlite3'

step2

npm install electron-rebuild --save-dev

electron-rebuild -f -w sqlite3

electron .

it worked.

but if you used 'electron-packager' to package and make executable file, It didn't work. the same error occured.

"electron-packager . electron-tutorial-app --overwrite --asar --platform=win32 --arch=ia32 --icon=assets/icons/win/icon.ico --prune=true --out=new --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"Electron Test\"",

step3

I spent pretty much time to figure out. At the end, I figured out. the solution was using electron-packager API and integrating afterCopy with electron-rebuild.

# build.js
const packager = require('electron-packager');
const rebuild = require('electron-rebuild');

packager({
    dir: '../dir',
    overwrite: true,
    asar: true,
    platform: 'win32',
    arch: 'ia32',
    icon: 'assets/icons/win/icon.ico',
    prune: true,
    out: 'hello',
    executableName: 'hello',
    afterCopy: [(buildPath, electronVersion, platform, arch, callback) => {
    rebuild.rebuild({ buildPath, electronVersion, arch })
      .then(() => callback())
      .catch((error) => callback(error));
  }],
})

node build.js

reference

Upvotes: 3

Vadim Macagon
Vadim Macagon

Reputation: 14837

Doesn't the --ignore=\"ignore|bin|node_modules\" you pass to electron-packager ignore everything in the node_modules dir, the very place where the sqlite3 module is installed?

Upvotes: 1

Related Questions