Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to package sqlite3 in node.js executable packages?

I want to make use of a simple database in a compiled node.js app. Is this possible without installing the database separately? i.e I want the database to be included in the .exe file so that I can just copy and execute that file.

I'm using pkg to create .exe files and it works well, but when I use the sqlite3 npm module the .exe errors when I try to execute with the following warning:

pkg/prelude/bootstrap.js:1155
      throw error;
      ^

Error: Cannot find module 'C:\snapshot\sqlite\node_modules\sqlite3\lib\binding\node-v51-win32-x64\node_sqlite3.node'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._resolveFilename (pkg/prelude/bootstrap.js:1252:46)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at Module.require (pkg/prelude/bootstrap.js:1136:31)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\snapshot\sqlite\node_modules\sqlite3\lib\sqlite3.js:4:15)
    at Module._compile (pkg/prelude/bootstrap.js:1226:22)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)

Which looks similar to a sqlite3 bug that was fixed here: https://github.com/zeit/pkg/issues/183 (since the bug was fixed I assume this is a user issue)

Looking at the error message, it looks like the ...../bide_sqlit3.node file can't be found. And looking at node_modules/ in my development env (where the module works) I can't find that file. So I assume that the file is NOT being included in the executable and that I need to do something that:

  1. Makes pkg include the file in the binary
  2. Alters the path to the file to be the path to the file in the binary

How do I do this with zeit/pkg? or, if this is more correct: How do I force npm to to install binaries to node_modules and then reference those binaries?

like image 572
Zach Smith Avatar asked Sep 28 '17 08:09

Zach Smith


People also ask

Can I use SQLite with node js?

Node. js can be used very well with relational databases and SQLite is no exception.

What is sqlite3 package?

SQLite is a C library that provides a lightweight disk-based database that doesn't require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage.

What is the latest version of sqlite3?

2020-12-01 - Release 3.34. SQLite version 3.34.


1 Answers

You should put the built node-sqlite3.node in the same directory as the binary that you built with pkg (as stated in this issue). This file can be found in your node_modules/sqlite3/lib/binding/node-vxx-xxxxx-xxx/node_sqlite3.node.

You also need to make sure that you build your project with pkg with the same node version that you built sqlite3 with.

like image 144
Martin Klomp Avatar answered Nov 15 '22 07:11

Martin Klomp