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

标签 node.js sqlite 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)

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

看报错信息,好像是找不到...../bide_sqlit3.node文件。在我的开发环境(模块工作的地方)中查看 node_modules/ 我找不到该文件。所以我假设该文件没有包含在可执行文件中,我需要做一些事情:

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

如何使用 zeit/pkg 执行此操作?或者,如果这更正确:How do I force npm to to install binaries to node_modules and then reference those binaries?

最佳答案

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

您还需要确保使用与构建 sqlite3 时使用的 Node 版本相同的 pkg 构建项目。

关于node.js - 如何将sqlite3打包到node.js可执行包中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46465037/

相关文章:

google-app-engine - 我可以在谷歌应用引擎上使用 node.js npm 和 socket.io

node.js - 安装 Node 模块时 node-gyp 重建失败

javascript - 为node保存sqlite3包的结果

mysql - 由于 appcelerator 中错误的字符串连接而导致参数化执行查询中断

android - 如何在多个 Activity 中添加数据

node.js - 将 node.js 应用程序打包为跨平台可执行文件,不适用于桌面应用程序

angularjs - AngularJS 和 NodeJs 的套接字问题

javascript - 获取 redis 列表中特定字符串的索引?

node.js - zeit/pkg 如何捆绑我的 Node 应用程序?