preface

I am using a computer with Apple M1 chip. Recently, I am making a desktop client based on electron development. The database uses SQlite3, but sqlite3 installed in node environment cannot be used in electron environment, so it needs to be recompiled. And there is no compiled ARM64 version of SQlite3 😨😓😭 on the NPM image

There is no way, only their own manual compilation, after some groping, finally successfully compiled sqlite3 can be used in the electron environment, their own hands, abundant food and clothing 😁😃😆

Compile environment

  • MacOS 11.5.2(Apple M1 Chip)
  • The node 16.14.0 (arm64)
  • Electron 16.0.6
  • Node – gyp 8.4.1

The preparatory work

Global installation electron

NPM install [email protected] - g

Install Node-gyp globally

npm install node-gyp -g

Compile and install the SQlite3 module

Make sure you have arm64 node installed at nodejs.org/en/download

  • Download the SQlite3 module

npm install sqlite3 --ignore-scripts

–ignore-scripts is added because the sqlite3 module writes a script in the package.json file that is automatically compiled on download as follows:

  "scripts": {
    "install": "node-pre-gyp install --fallback-to-build". }Copy the code

Often our compilation environment is not ready, and if the compilation fails, the entire package will not be downloaded.

  • Manually execute the script to compile
cd node_modules/sqlite3
node-gyp rebuild --target=16.06. --arch=arm64 --dist-url=https://electronjs.org/headers --module_name=node_sqlite3 --module_path=.. /lib/binding/napi-v3-darwin-arm64
Copy the code

–target=16.0.6 // Specify the electron version to 16.0.6

Arch =arm64 // Specifies the build architecture to be arm64

–dist-url= XXX // Related dependencies download address

–module_name=node_sqlite3 // Specify the module name as node_sqlite3

–module_path=.. /lib/binding/ api-v3-darwin-arm64 // Compile the output path

If an error message is displayed during compilation: libtool: unrecognized option-static, run the following command:

export PATH=/Library/Developer/CommandLineTools/usr/bin:$PATH
Copy the code

Verify the sqlite3

const sqlite3=require('sqlite3')

let db = new sqlite3.Database('sq3.db'.(err) = > {
   if (err) {
    return console.error(err.message);
   }
   console.log('Successfully connected to SQLite database');
});
Copy the code

A complete, runnable Electron demo has been uploaded to GitHub at github.com/kongpf8848/…

reference

www.npmjs.com

www.electronjs.org

github.com/electron

Github.com/mapbox/node…