11

Question

Is there a way to install node-sqlite3 for multiple platforms I am targeting in my app without running standalone build for just every target platform combination?

Context

In my Node.js app I have a npm dependency node-sqlite3 (GitHub, npm), which contains different binaries (bindings) for different platforms.

My app is targeting different platforms, including Windows, Linux and macOS (both ia32 and x64) and modern Node versions: v6, v7 and v8. The app doesn't have any platform-specific behavior.

If I install the project's dependencies using npm install, node-sqlite3 downloads binaries just for the current platform (let's say win32, x64, Node v7.10).

I also have a Travis CI build configuration, which I use for Continuous Deployment as well as Continuous Integration. I chose Ubuntu Trusty as a host for executing builds.

As a part of a build process the app's dependencies are being installed by npm install. Within deployment process, the built app with it's dependencies is being packaged (archived) and uploaded to a file hosting for further distribution.

Issue

node-sqlite3 is not installed for all target platforms I need, but just for a platform currently being used (for development or executing a build).

Possible solution

I could execute builds and deploy:

  • with Travis - for Linux and macOS
  • with AppVeyor - for Windows

But that's looks like a big overhead. As I've already said, the app doesn't have any platform-specific behavior. And I trust node-sqlite3's vendor tested it at all major platforms I am targeting.

Deilan
  • 4,740
  • 3
  • 39
  • 52

1 Answers1

16

Yes, in case with node-sqlite3 you do have such a capability.

It is possible because it's owner mapbox uses node-pre-gyp (GitHub, npm) for distribution of node-sqlite3.

After installing your app's dependencies with npm install execute the following command at the root of your Node project for every target platform combination:

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

As a result, you will have required bindings in the ./node_modules/sqlite3/lib/binding/ directory.

Options

Here's the options' descriptions from the 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

If they exist, prebuilt binaries for chosen platform will be downloaded from a file storage (Amazon S3). Otherwise you have to build binaries by yourself.

A list of available binaries of node-sqlite3 is here.

Examples

A couple of examples for certain target platforms:

• Windows x86 and 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 and 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 and 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
giraffesyo
  • 4,860
  • 1
  • 29
  • 39
Deilan
  • 4,740
  • 3
  • 39
  • 52
  • Something that confused me, but I figured out - my error message was talking about missing `lib/binding/node-v48-linux-x64/node_sqlite3.node`. Even though it says `node-v48` here, you should just use whatever the output is from `node -v` as the argument. In my case it was `6.9.4` – Zach Thacker Oct 18 '17 at 04:08
  • hi @Deilan, could you please provide the Example for linux armv7 – krishna Apr 01 '21 at 07:04
  • You can find the node-sqlite3 releases at https://github.com/TryGhost/node-sqlite3/releases/. You can then match which version and target parameters you need from your error message, example: `Error: Cannot find module 'C:\snapshot\modbus\node_modules\sqlite3\lib\binding\napi-v6-win32-unknown-x64\node_sqlite3.node'` To include the windows sqlite3 node within your linux build process you would do: `/node_modules/.bin/node-pre-gyp install --directory=./node_modules/sqlite3 --target_platform=win32 --target_arch=x64 --target=16.15.0 --target_libc=unknown` – ben Jul 16 '22 at 16:47