Cross platform npm sqlite3 package installation
Question
Is there a way to install node-sqlite3
for multiple platforms that I am targeting in my application without running a standalone build just for each combination of target platforms?
Context
In my Node.js application, I have an npm dependency node-sqlite3
( GitHub , npm ) that contains different binaries (bindings) for different platforms.
My application targets multiple platforms, including Windows , Linux and macOS (both ia32 and x64 ) and modern Node versions: v6, v7, and v8. The app does not have any platform-specific actions.
If I set the project dependencies using npm install
, node-sqlite3
loads binary files for the current platform (eg win32
, x64
, Node v7.10
).
I also have a Travis CI build configuration that I use for continuous deployment as well as continuous integration. I chose Ubuntu Trusty as the host for doing the builds.
As part of the build process, application dependencies are installed on npm install
. During the deployment process, the embedded application with its dependencies is packaged (archived) and uploaded to the file hosting for further distribution.
Question
node-sqlite3
not installed for all the target platforms I need, but only for the platform currently in use (for developing or doing a build).
Possible Solution
I can build and deploy:
- with Travis - for Linux and macOS
- with AppVeyor - for Windows
But that looks like a lot of overhead. As I said, the application does not have any platform-specific behavior. And I believe the node-sqlite3
vendor has tested it on all major platforms that I am targeting.
source to share
Yes , in the case node-sqlite3
of you, you have such an opportunity.
This is possible because the owner of the mapbox uses node-pre-gyp
( GitHub , npm ) for distribution node-sqlite3
.
After installing your application's dependencies, npm install
run the following command in the root directory of your Node project for each combination of target platforms :
./node_modules/.bin/node-pre-gyp install
--directory=./node_modules/sqlite3
--target_platform={OS}
--target_arch={OS architecture}
--target={Node version}
(note that line breaks here are just for clarity, you need to remove or avoid them before executing)
As a result, you need a directory binding ./node_modules/sqlite3/lib/binding/
.
Functions
Here's the descriptions of the options from the node-pre-gyp docs .
- directory : run a 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 in the target arch and override the arch of the node. Valid values ββare "ia32", "x64", or "hand".
- target = 0.10.25 : pass a target to Node or node-webkit to compile with
If they exist , the pre-built binaries for the selected platform will be loaded from the file store (Amazon S3). Otherwise, you must create the binaries yourself.
The list of available binaries is node-sqlite3
here .
Examples of
Some examples for specific 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
source to share