如何将better sqlite3的node gyp原生模块编译prebuild-install改写成长尾?

2026-04-02 20:521阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1057个文字,预计阅读时间需要5分钟。

如何将better sqlite3的node gyp原生模块编译prebuild-install改写成长尾?

目录 + 关于 node-gyp、prebuild-install 和 better-sqlite3 安装过程追踪

1.安装 better-sqlite3

2.better-sqlite3 的 package.json 命令脚本

3.prebuild-install 在 package.json 中的配置

4.下载预构建的二进制文件,让 prebuild-install 通过淘宝源

目录
  • 关于node-gyp
  • prebuild-install
  • better-sqlite3安装过程追踪
    • 1. 安装 better-sqlite3
    • 2. better-sqlite3的package的命令脚本
    • 3. prebuild-install的package.json
    • 4. 下载预构建的二进制文件
  • 让prebuild-install通过淘宝源下载预构建文件

    关于node-gyp

    node-gyp是一个用 Node.js 编写的跨平台命令行工具,用于为 Node.js 编译本机插件模块。它包含之前由 Chromium 团队使用的 gyp-next项目的供应副本,扩展以支持 Node.js 原生插件的开发。

    node-gypis a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js. It contains a vendored copy of thegyp-nextproject that was previously used by the Chromium team, extended to support the development of Node.js native addons.

    node是跨平台的,那么对于任何的node模块理论也是应该是跨平台的。然而,有些node模块直接或间接使用原生C/C++代码,这些东西要跨平台,就需要使用源码根据实际的操作平台环境进行原生模块编译。通常我们开发环境为macOS或Windows,而生产环境为Linux的各种发行版,这将导致我们的开发工作变得沉重不堪。那我们是否可以跳过node-gyp的编译过程?

    prebuild-install

    node-gyp的编译是让人难受的过程,所以社区出现了node-pre-gypprebuild-install,它们都会优先下载插件作者预编译的二进制文件,当二进制文件下载出现问题时,再使用node-gyp进行编译兜底。但因为我们网络环境的特殊性,这些二进制文件我们大概率是不会下载成功的,接下来一起来看看在better-sqlite3的安装过程中prebuild-install干了什么事。

    关于node-pre-gyp参考姊妹文关于原生模块编译node-gyp + node-pre-gyp (以安装canvas为例)

    better-sqlite3安装过程追踪

    better-sqlite3就使用了prebuild-install来优化构建过程

    如何将better sqlite3的node gyp原生模块编译prebuild-install改写成长尾?

    1. 安装 better-sqlite3

    关于install我们需要了解一点东西, 通常基于表象我们都会认为npm下载解压并释放到node_modules后就完成了安装过程,但实际上npm还会检查package中是否配置了install命令,存在就会立即执行。

    npm install better-sqlite3

    2. better-sqlite3的package的命令脚本

    可以看到better-sqlite3配置了install命令,所以npm下载better-sqlite3后立即执行了prebuild-install

    { ..., "scripts": { "install": "prebuild-install || npm run build-release", "build-release": "node-gyp rebuild --release", "build-debug": "node-gyp rebuild --debug", "rebuild-release": "npm run lzz && npm run build-release", "rebuild-debug": "npm run lzz && npm run build-debug", "test": "mocha --exit --slow=75 --timeout=5000", "benchmark": "node benchmark", "download": "bash ./deps/download.sh", "lzz": "lzz -hx hpp -sx cpp -k BETTER_SQLITE3 -d -hl -sl -e ./src/better_sqlite3.lzz" }, }

    3. prebuild-install的package.json

    可以看到prebuild-install使用bin将prebuild-install命令链接到了bin.js

    { ..., "bin": "./bin.js", ... }

    4. 下载预构建的二进制文件

    可以看到bin.js中核心函数就是startDownload

    const startDownload = function (downloadUrl) { download(downloadUrl, opts, function (err) { if (err) { log.warn('install', err.message) return process.exit(1) } log.info('install', 'Successfully installed prebuilt binary!') }) } if (opts.token) { asset(opts, function (err, assetId) { if (err) { log.warn('install', err.message) return process.exit(1) } startDownload(util.getAssetUrl(opts, assetId)) }) } else { startDownload(util.getDownloadUrl(opts)) }

    其中opts的核心参数项如下。粘贴的代码有删减,完整的请自行查阅源码

    const rc = require('rc')('prebuild-install', { target: pkgConf.target || env.npm_config_target || process.versions.node, runtime: pkgConf.runtime || env.npm_config_runtime || 'node', arch: pkgConf.arch || env.npm_config_arch || process.arch, libc: libc, platform: env.npm_config_platform || process.platform, debug: env.npm_config_debug === 'true', force: false, verbose: env.npm_config_verbose === 'true', buildFromSource: buildFromSource === pkg.name || buildFromSource === 'true', path: '.', proxy: env.npm_config_proxy || env.registry.npmmirror.com/-/binary/better-sqlite3 better_sqlite3_binary_host_mirror=registry.npmmirror.com/-/binary/better-sqlite3 better-sqlite3_binary_host=registry.npmmirror.com/-/binary/better-sqlite3 better-sqlite3_binary_host_mirror=registry.npmmirror.com/-/binary/better-sqlite3

    鉴于node-pre-gyp只读取{包名}_binary_host_mirror,针对原生模块我们在.npmrc中以{包名}_binary_host_mirror={mirror}的格式配置预构建文件下载镜像

    以上就是better sqlite3安装node gyp原生模块编译prebuild-install的详细内容,更多关于node 模块编译prebuild install的资料请关注自由互联其它相关文章!

    本文共计1057个文字,预计阅读时间需要5分钟。

    如何将better sqlite3的node gyp原生模块编译prebuild-install改写成长尾?

    目录 + 关于 node-gyp、prebuild-install 和 better-sqlite3 安装过程追踪

    1.安装 better-sqlite3

    2.better-sqlite3 的 package.json 命令脚本

    3.prebuild-install 在 package.json 中的配置

    4.下载预构建的二进制文件,让 prebuild-install 通过淘宝源

    目录
    • 关于node-gyp
    • prebuild-install
    • better-sqlite3安装过程追踪
      • 1. 安装 better-sqlite3
      • 2. better-sqlite3的package的命令脚本
      • 3. prebuild-install的package.json
      • 4. 下载预构建的二进制文件
    • 让prebuild-install通过淘宝源下载预构建文件

      关于node-gyp

      node-gyp是一个用 Node.js 编写的跨平台命令行工具,用于为 Node.js 编译本机插件模块。它包含之前由 Chromium 团队使用的 gyp-next项目的供应副本,扩展以支持 Node.js 原生插件的开发。

      node-gypis a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js. It contains a vendored copy of thegyp-nextproject that was previously used by the Chromium team, extended to support the development of Node.js native addons.

      node是跨平台的,那么对于任何的node模块理论也是应该是跨平台的。然而,有些node模块直接或间接使用原生C/C++代码,这些东西要跨平台,就需要使用源码根据实际的操作平台环境进行原生模块编译。通常我们开发环境为macOS或Windows,而生产环境为Linux的各种发行版,这将导致我们的开发工作变得沉重不堪。那我们是否可以跳过node-gyp的编译过程?

      prebuild-install

      node-gyp的编译是让人难受的过程,所以社区出现了node-pre-gypprebuild-install,它们都会优先下载插件作者预编译的二进制文件,当二进制文件下载出现问题时,再使用node-gyp进行编译兜底。但因为我们网络环境的特殊性,这些二进制文件我们大概率是不会下载成功的,接下来一起来看看在better-sqlite3的安装过程中prebuild-install干了什么事。

      关于node-pre-gyp参考姊妹文关于原生模块编译node-gyp + node-pre-gyp (以安装canvas为例)

      better-sqlite3安装过程追踪

      better-sqlite3就使用了prebuild-install来优化构建过程

      如何将better sqlite3的node gyp原生模块编译prebuild-install改写成长尾?

      1. 安装 better-sqlite3

      关于install我们需要了解一点东西, 通常基于表象我们都会认为npm下载解压并释放到node_modules后就完成了安装过程,但实际上npm还会检查package中是否配置了install命令,存在就会立即执行。

      npm install better-sqlite3

      2. better-sqlite3的package的命令脚本

      可以看到better-sqlite3配置了install命令,所以npm下载better-sqlite3后立即执行了prebuild-install

      { ..., "scripts": { "install": "prebuild-install || npm run build-release", "build-release": "node-gyp rebuild --release", "build-debug": "node-gyp rebuild --debug", "rebuild-release": "npm run lzz && npm run build-release", "rebuild-debug": "npm run lzz && npm run build-debug", "test": "mocha --exit --slow=75 --timeout=5000", "benchmark": "node benchmark", "download": "bash ./deps/download.sh", "lzz": "lzz -hx hpp -sx cpp -k BETTER_SQLITE3 -d -hl -sl -e ./src/better_sqlite3.lzz" }, }

      3. prebuild-install的package.json

      可以看到prebuild-install使用bin将prebuild-install命令链接到了bin.js

      { ..., "bin": "./bin.js", ... }

      4. 下载预构建的二进制文件

      可以看到bin.js中核心函数就是startDownload

      const startDownload = function (downloadUrl) { download(downloadUrl, opts, function (err) { if (err) { log.warn('install', err.message) return process.exit(1) } log.info('install', 'Successfully installed prebuilt binary!') }) } if (opts.token) { asset(opts, function (err, assetId) { if (err) { log.warn('install', err.message) return process.exit(1) } startDownload(util.getAssetUrl(opts, assetId)) }) } else { startDownload(util.getDownloadUrl(opts)) }

      其中opts的核心参数项如下。粘贴的代码有删减,完整的请自行查阅源码

      const rc = require('rc')('prebuild-install', { target: pkgConf.target || env.npm_config_target || process.versions.node, runtime: pkgConf.runtime || env.npm_config_runtime || 'node', arch: pkgConf.arch || env.npm_config_arch || process.arch, libc: libc, platform: env.npm_config_platform || process.platform, debug: env.npm_config_debug === 'true', force: false, verbose: env.npm_config_verbose === 'true', buildFromSource: buildFromSource === pkg.name || buildFromSource === 'true', path: '.', proxy: env.npm_config_proxy || env.registry.npmmirror.com/-/binary/better-sqlite3 better_sqlite3_binary_host_mirror=registry.npmmirror.com/-/binary/better-sqlite3 better-sqlite3_binary_host=registry.npmmirror.com/-/binary/better-sqlite3 better-sqlite3_binary_host_mirror=registry.npmmirror.com/-/binary/better-sqlite3

      鉴于node-pre-gyp只读取{包名}_binary_host_mirror,针对原生模块我们在.npmrc中以{包名}_binary_host_mirror={mirror}的格式配置预构建文件下载镜像

      以上就是better sqlite3安装node gyp原生模块编译prebuild-install的详细内容,更多关于node 模块编译prebuild install的资料请关注自由互联其它相关文章!