Search code examples
node.jsnpmcross-platformtravis-cinode-sqlite3

Cross-platform install of npm package sqlite3


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.


Solution

  • 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