jctaoo / vite-electron-esbuild-starter

⚡️The electron starter using Vite and esbuild to fast development.
MIT License
54 stars 7 forks source link

Cannot find moudle 'sqlite3', please support #53

Open newonejoe opened 3 years ago

newonejoe commented 3 years ago

Hi @jctaoo, can you do me a favor to show how to use sqlite3 with this template. Install the sqlite 3 with following command yarn add sqlite3@5.0.0 --save. There is no problem during yarn dev . After yarn pack:mac or yarn pack:win, the building and packing is also successful. But there is always annoying error message: "Error cannot find module 'sqlite3'" after running the application

image

Nothing special in the ./src/main/utils/db.ts

import * as sqlite3 from 'sqlite3';
export const init = ()=> {
  let db = new sqlite3.Database('app.db');
  db.serialize(function() {
  db.run("CREATE TABLE if not exists lorem (info TEXT)");

  var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
  for (var i = 0; i < 10; i++) {
      stmt.run("Ipsum " + i);
  }
  stmt.finalize();

  db.each("SELECT rowid AS id, info FROM lorem", function(err:any, row:any) {
      console.log(row.id + ": " + row.info);
    });
  });
  db.close();
}

After googling, there are lot of articles about electron-rebuild -f -w sqlite3. I add one line to package.json scripts "rebuild_sqlite3": "electron-rebuild -f -w sqlite3", and running yarn rebuild_sqlite3. It's working and generate napi-v{napi_build_version}-win32-x64 at \node_modules\sqlite3\lib\binding. Following is the log for rebuild_sqlite3

PS C:\Users\cn101143\source\repos\electron\qtool-logger-antd> yarn rebuild_sqlite3 yarn run v1.22.11 $ electron-rebuild -f -w sqlite3 ⠹ Building module: sqlite3, Completed: 0gyp info find Python using Python version 3.10.0 found at "C:\Python310\python.exe" ⠧ Building module: sqlite3, Completed: 0gyp info find VS using VS2019 (16.9.31229.75) found at: gyp info find VS "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community" gyp info find VS run with --verbose for detailed information gyp info spawn C:\Python310\python.exe gyp info spawn args [ gyp info spawn args 'C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\electron-rebuild\node_modules\node-gyp\gyp\gyp_main.py', gyp info spawn args 'binding.gyp', gyp info spawn args '-f', gyp info spawn args 'msvs', gyp info spawn args '-I', gyp info spawn args 'C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\sqlite3\build\config.gypi', gyp info spawn args '-I', gyp info spawn args 'C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\electron-rebuild\node_modules\node-gyp\addon.gypi', gyp info spawn args '-I', gyp info spawn args 'C:\Users\cn101143\.electron-gyp\11.3.0\include\node\common.gypi', gyp info spawn args '-Dlibrary=shared_library', gyp info spawn args '-Dvisibility=default', gyp info spawn args '-Dnode_root_dir=C:\Users\cn101143\.electron-gyp\11.3.0', gyp info spawn args '-Dnode_gyp_dir=C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\electron-rebuild\node_modules\node-gyp', gyp info spawn args '-Dnode_lib_file=C:\\Users\\cn101143\\.electron-gyp\\11.3.0\\<(target_arch)\\node.lib', gyp info spawn args '-Dmodule_root_dir=C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\sqlite3', gyp info spawn args '-Dnode_engine=v8', gyp info spawn args '--depth=.', gyp info spawn args '--no-parallel', gyp info spawn args '--generator-output', gyp info spawn args 'C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\sqlite3\build', gyp info spawn args '-Goutput_dir=.' gyp info spawn args ] ⠴ Building module: sqlite3, Completed: 0gyp info spawn C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe gyp info spawn args [ gyp info spawn args 'build/binding.sln', gyp info spawn args '/clp:Verbosity=minimal', gyp info spawn args '/nologo', gyp info spawn args '/p:Configuration=Release;Platform=x64', gyp info spawn args map: [Function (anonymous)] gyp info spawn args ] ⠦ Building module: sqlite3, Completed: 0Building the projects in this solution one at a time. To enable parallel build, please add the "-m" switch. ⠋ Building module: sqlite3, Completed: 0 nothing.c ⠼ Building module: sqlite3, Completed: 0 win_delay_load_hook.cc ⠧ Building module: sqlite3, Completed: 0 nothing.vcxproj -> C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\sqlite3\build\Release\noth ing.lib ⠏ Building module: sqlite3, Completed: 0 unpack_sqlite_dep ⠋ Building module: sqlite3, Completed: 0 win_delay_load_hook.cc ⠼ Building module: sqlite3, Completed: 0 sqlite3.vcxproj -> C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\sqlite3\build\Release\sqli te3.lib ⠦ Building module: sqlite3, Completed: 0 backup.cc ⠴ Building module: sqlite3, Completed: 0 database.cc ⠴ Building module: sqlite3, Completed: 0 node_sqlite3.cc ⠼ Building module: sqlite3, Completed: 0 statement.cc ⠼ Building module: sqlite3, Completed: 0 win_delay_load_hook.cc ⠏ Building module: sqlite3, Completed: 0 Creating library C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\sqlite3\build\Release\node _sqlite3.lib and object C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\sqlite3\build\Release\ node_sqlite3.exp Generating code Previous IPDB not found, fall back to full compilation. ⠙ Building module: sqlite3, Completed: 0 All 4503 functions were compiled because no usable IPDB/IOBJ from previous compilation was found. Finished generating code ⠦ Building module: sqlite3, Completed: 0 node_sqlite3.vcxproj -> C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\sqlite3\build\Release\ \node_sqlite3.node ⠧ Building module: sqlite3, Completed: 0 Copying C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\sqlite3\build\Release\/node_sqlite3.no de to C:\Users\cn101143\source\repos\electron\qtool-logger-antd\node_modules\sqlite3\lib\binding\napi-v{napi_build_ve rsion}-win32-x64 1 file(s) copied. √ Rebuild Complete Done in 16.07s.

Can you demonstrate how to use sqlite3?

Brs

Joe

jctaoo commented 3 years ago

I’ll create a simple repo integrated with SQLite from this template later.

Foreinyel commented 2 years ago

The problem is encountered in main process. This starter uses electron-run to build index.js of main process. Inside electron-run, it's esbuild. So you have to change the esbuild options in the node_modules/electron-run/build/main/src/commands/esbuild.js.

try {
        await esbuild.build({
            outdir: outDir,
            entryPoints: entryPoints,
            tsconfig: tsconfigPath,
            format: 'cjs',
            logLevel: 'silent',
            logLimit: 0,
            incremental: !isBuild,
            platform: 'node',
            sourcemap: true,
            bundle: true,
            external: externals,
            watch: !isBuild
                ? {
                    onRebuild: async (error) => {
                        if (error) {
                            reportError(...transformErrors(error));
                        }
                        else {
                            count++;
                            buildComplete(outDir, count);
                        }
                    },
                }
                : false,
        });
        count++;
        buildComplete(outDir, count);
    }

As you can see, the options of esbuild are not smart enough. The sqlite3 you imported is excluded in the bundle file. But you should only exclude the electron module. If you would use other modules in main process, you should change the external: external to external: ['electron'].