如何将sqlite3打包到node.js可执行包中?

Zac*_*ith 2 sqlite node.js zeit-pkg

我想在已编译的node.js应用程序中使用一个简单的数据库。无需单独安装数据库是否可以实现?即我希望数据库包含在.exe文件中,以便我可以复制并执行该文件。

我正在使用pkg创建.exe文件,并且效果很好,但是当我使用sqlite3 npm模块尝试执行以下警告时,.exe错误出现了:

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)
Run Code Online (Sandbox Code Playgroud)

看起来与在此处修复的sqlite3错误类似:https//github.com/zeit/pkg/issues/183(由于该错误已修复,因此我认为这是用户问题)

查看错误消息,似乎...../bide_sqlit3.node找不到该文件。而看着node_modules/我的发展ENV(其中模块工作),我无法找到该文件。因此,我假定该文件未包含在可执行文件中,并且我需要执行以下操作:

  1. 使pkg文件包含在二进制文件中
  2. 将文件路径更改为二进制文件中的文件路径

我该怎么做zeit/pkg?或者,如果更正确:我如何强制npm将二进制文件安装到node_modules,然后引用这些二进制文件?

Mar*_*omp 6

您应该将构建的node-sqlite3.node与使用pkg构建的二进制文件放在同一目录中(如本期所述)。可以在您的node_modules / sqlite3 / lib / binding / node-vxx-xxxxx-xxx / node_sqlite3.node中找到该文件。

您还需要确保使用pkg构建项目,并使用与构建sqlite3相同的节点版本。

  • @ simon-pr:我的回答也反映了pkg手册(https://github.com/zeit/pkg#native-addons),因此您的问题可能就是pkg中的错误。 (2认同)