2

I want to use a child_process to perform a long task in the background without slowing down the whole application.

I'm having trouble requiring the sqlite3 module inside of my child_process. Here's my code:

main.js

const child_process = require('child_process');
const fork = child_process.fork('child.js');

child.js

const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('data.db');

Here's what I get:

module.js:487 throw err; ^

Error: Cannot find module 'A:\myretail\clictill_electron\node_modules\sqlite3\lib\binding\node-v57-win32-x64\node_sqlite3.node'
    at Function.Module._resolveFilename (module.js:485:15)
    at Function.Module._load (module.js:437:25)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (A:\myretail\clictill_electron\node_modules\sqlite3\lib\sqlite3.js:4:15)
    at Object.<anonymous> (A:\myretail\clictill_electron\node_modules\sqlite3\lib\sqlite3.js:190:3)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)

package.json:

{
    "name": "clictill_electron",
    "productName": "Clictill",
    "version": "1.0.0",
    "main": "main.js",
    "dependencies": {
        "child_process": "^1.0.2",
        "electron": "^1.8.4",
        "fibers": "^2.0.2",
        "node-gyp": "^3.6.2",
        "node-pre-gyp": "^0.8.0",
        "request": "^2.85.0",
        "sqlite3": "^3.1.13",
        "sync": "^0.2.5"
    },
    "build": {
        "files": [
            "app/**/*",
            "node_modules/**/*",
            "package.json"
        ]
    },
    "scripts": {
        "preinstall": "npm --add-python-to-path='true' --debug install --global windows-build-tools && npm install --global node-gyp && setx PYTHON \"%USERPROFILE%\\.windows-build-tools\\python27\\python.exe\" && npm config set python PYTHON",
        "postinstall": "node_modules/.bin/electron-rebuild -v 1.8.2 -w sqlite3 -p",
        "start": "electron .",
        "release": "build",
        "rebuild": "electron-rebuild -f -w sqlite3"
    },
    "devDependencies": {
        "electron-builder": "^10.8.1",
        "electron-rebuild": "^1.7.3"
    }
}

Any idea where this is coming from?

Dago
  • 31
  • 1
  • 1
  • 4
  • You have run `npm install` correct? – zero298 Apr 20 '18 at 14:16
  • Yes, my app is working correctly, I can require every module everywhere, I just have trouble with the sqlite3 module when it's required inside a child_process – Dago Apr 20 '18 at 14:29
  • Possible duplicate of [How to use sqlite3 module with electron?](https://stackoverflow.com/questions/32504307/how-to-use-sqlite3-module-with-electron) – zero298 Apr 20 '18 at 14:31
  • This is not the same problem, I got my app working with SQLite, my only problem is when I'm trying to use SQLite **inside a child_process** – Dago Apr 20 '18 at 14:33
  • Did you find a solution to this problem? I am having the same issue (although in my case it's cypress complaining about not finding electron) – Luxalpa Sep 13 '22 at 22:08

1 Answers1

-3

Have you checked your path to see if the sqlite3 module exists where it should?

otherwise i would do npm child_process uninstall --save, then do npm child_process install --save

Pygirl
  • 12,969
  • 5
  • 30
  • 43
  • Hi, and thanks for such a quick reply! I just checked the path, and the file actually is in the folder "node-v**59**-win32-x64" instead of "node-v**57**-win32-x64". What does this number represent? (sorry I'm a very new Node user) – Dago Apr 20 '18 at 14:15
  • it is the node version package you are using for the project, so in your package.json, looks like it is trying to look v57 in v59 folder. If you ammend your package.json to suit it should run – Who Me Dunno Mate Apr 20 '18 at 14:20
  • Also I tried to reinstall the child_process module as you suggested, I have the same result – Dago Apr 20 '18 at 14:20
  • I don't see anything weird in package.json, is there a way to manually specify the node version package somewhere so that it looks in the right folder? – Dago Apr 20 '18 at 14:23
  • This gives the same result :( – Dago Apr 20 '18 at 14:45
  • try npm install sqlite3 --build-from-source – Who Me Dunno Mate Apr 20 '18 at 14:49
  • Still not working. I also tried removing the node_modules folder and running npm install, still gives me the same problem... – Dago Apr 20 '18 at 14:52
  • npm install --save-dev electron-rebuild – Who Me Dunno Mate Apr 20 '18 at 14:57
  • try the following in sequence https://www.laurivan.com/make-electron-work-with-sqlite3/ – Who Me Dunno Mate Apr 20 '18 at 14:57
  • This gives me the same error, with the wrong node version. I'm really lost on this, because SQLite works perfectly fine in my app, unless it's required in a child process... – Dago Apr 23 '18 at 07:37
  • This is incorrect child_process is a built in module. – Alexis Tyler Aug 16 '21 at 06:18