node.js - 跨平台安装 npm 包 sqlite3

标签 node.js npm cross-platform travis-ci node-sqlite3

问题

有没有办法为我在我的应用程序中定位的多个平台安装 node-sqlite3,而无需为每个目标平台组合运行独立构建?

上下文

在我的 Node.js 应用程序中,我有一个 npm 依赖项 node-sqlite3(GitHubnpm),它包含针对不同平台的不同二进制文件(绑定(bind))。

我的应用面向不同的平台,包括 WindowsLinuxma​​cOS(ia32x64) 和现代 Node 版本:v6、v7 和 v8。该应用没有任何特定于平台的行为。

如果我使用 npm install 安装项目的依赖项,node-sqlite3 只会为当前平台下载二进制文件(比如 win32x64, Node v7.10).

我还有一个 Travis CI 构建配置,我将其用于持续部署和持续集成。我选择了Ubuntu Trusty作为执行构建的主机。

作为构建过程的一部分,应用程序的依赖项正在通过 npm install 安装。在部署过程中,构建的应用程序及其依赖项被打包(存档)并上传到文件托管以供进一步分发。

问题

node-sqlite3 并没有为我需要的所有目标平台安装,而只是为当前正在使用的平台(用于开发或执行构建)安装。

可能的解决方案

我可以执行构建和部署:

  • 使用 Travis - 适用于 Linux 和 macOS
  • 使用 AppVeyor - 适用于 Windows

但这看起来是一个很大的开销。正如我已经说过的,该应用程序没有任何特定于平台的行为。我相信 node-sqlite3 的供应商已在我所针对的所有主要平台上对其进行了测试。

最佳答案

是的,如果使用 node-sqlite3,你确实有这样的能力。

这是可能的,因为它的所有者mapbox使用 node-pre-gyp ( GitHub , npm ) 分发 node-sqlite3

在使用 npm install 安装应用的依赖项后,针对每个目标平台组合,在 Node 项目的根目录下执行以下命令:

./node_modules/.bin/node-pre-gyp install
--directory=./node_modules/sqlite3
--target_platform={OS}
--target_arch={OS architecture}
--target={Node version}

(请注意,这里的换行符只是为了清楚起见,您必须在执行前删除或转义它们)

因此,您需要在 ./node_modules/sqlite3/lib/binding/ 目录中进行绑定(bind)。

选项

这是来自 node-pre-gyp docs 的选项描述.

--directory: run the command in this directory

--target_platform=win32: Pass the target platform and override the host platform. Valid values are linux, darwin, win32, sunos, freebsd, openbsd, and aix.

--target_arch=ia32: Pass the target arch and override the host arch. Valid values are 'ia32','x64', or arm.

--target=0.10.25: Pass the target node or node-webkit version to compile against

如果它们存在,将为所选平台预构建的二进制文件将从文件存储 (Amazon S3) 下载。否则,您必须自己构建二进制文件。

node-sqlite3 的可用二进制文件列表是 here .

例子

针对特定目标平台的几个示例:

• Windows x86 和 Node 6.10.0:

./node_modules/.bin/node-pre-gyp install --directory=./node_modules/sqlite3 --target_platform=win32 --target_arch=ia32 --target=6.10.0

• macOS x64 和 Node 7.10.0:

./node_modules/.bin/node-pre-gyp install --directory=./node_modules/sqlite3 --target_platform=darwin--target_arch=x64 --target=7.10.0

• Linux x64 和 Node 8.0.0:

./node_modules/.bin/node-pre-gyp install --directory=./node_modules/sqlite3 --target_platform=linux--target_arch=x64 --target=8.0.0

关于node.js - 跨平台安装 npm 包 sqlite3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45192449/

相关文章:

javascript - VSCode 调试器弄乱了相对路径

java - 是否有任何远程跨平台方式来执行外部进程?

javascript - 拖放功能在带有 nodejs (javascript) 测试自动化框架的 selenium webdriver 中不起作用

javascript - bluebird - 函数返回 promise 对象而不是实际数据

node.js - 如何使用 NodeJS 向 mongodb 执行批量插入

node.js - 确定从正在运行的 node.js 应用程序使用的 NPM 模块

node.js - Npm:无法调用未定义的方法 'charAt'

mysql - 如何在 SEQUELIZE (nodeJS) 中创建触发器?

android - 科尼 : Ant build error=exec-shell returned: 1

c++ - 实现资源目录的良好做法