无法将 sqlite3 编译为本机插件,Electron React Boilerplate 项目

Can't compile sqlite3 as a native addon, Electron React Boilerplate project

我正在构建一个基于 Electron React Boilerplate 项目的项目。我是 运行 MacOS 10.15.7 和节点 v14.15.1.

我正在尝试安装 sqlite3 包。因为它是本机依赖项,​​所以我在 src/ 目录中 运行 yarn add sqlite3,就像它说的那样 here。编译失败,输出如下:

henryoconnor@Henrys-MacBook-Pro-5 src % yarn add sqlite3                                                                                    
yarn add v1.22.10                                                                                                                    
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4]   Resolving packages...                                                                                                      
[2/4]   Fetching packages...                                                                                                       
[3/4]   Linking dependencies...                                                                                                    
[4/4]   Building fresh packages...                                                                                                 
success Saved lockfile.                                                                                                              
success Saved 1 new dependency.                                                                                                      
info Direct dependencies                                                                                                             
└─ sqlite3@5.0.2                                                                                                                     
info All dependencies                                                                                                                
└─ sqlite3@5.0.2
$ yarn electron-rebuild                                                                                                              
yarn run v1.22.10                                                                                                                    
$ node -r ../.erb/scripts/BabelRegister.js ../.erb/scripts/ElectronRebuild.js                                                        
⠴ Building modules: 0/1gyp info find Python using Python version 2.7.16 found at "/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python"
⠦ Building modules: 0/1gyp info spawn /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
gyp info spawn args [
gyp info spawn args   '/Users/henryoconnor/Documents/secretary/electron-app/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/henryoconnor/Documents/secretary/electron-app/src/node_modules/sqlite3/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/henryoconnor/Documents/secretary/electron-app/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/henryoconnor/.electron-gyp/11.3.0/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/henryoconnor/.electron-gyp/11.3.0',
gyp info spawn args   '-Dnode_gyp_dir=/Users/henryoconnor/Documents/secretary/electron-app/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/Users/henryoconnor/.electron-gyp/11.3.0/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/henryoconnor/Documents/secretary/electron-app/src/node_modules/sqlite3',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.'
gyp info spawn args ]
⠸ Building modules: 0/1No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'.                                              

No receipt for 'com.apple.pkg.DeveloperToolsCLILeo' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLI' found at '/'.

⠴ Building modules: 0/1gyp info spawn make                                                                                           
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
  CC(target) Release/obj.target/nothing/../node-addon-api/nothing.o
⠧ Building modules: 0/1  LIBTOOL-STATIC Release/nothing.a                                                                            
⠋ Building modules: 0/1  ACTION deps_sqlite3_gyp_action_before_build_target_unpack_sqlite_dep Release/obj/gen/sqlite-autoconf-3340000/sqlite3.c
⠼ Building modules: 0/1  TOUCH Release/obj.target/deps/action_before_build.stamp                                                     
⠴ Building modules: 0/1  CC(target) Release/obj.target/sqlite3/gen/sqlite-autoconf-3340000/sqlite3.o                                 
⠸ Building modules: 0/1  LIBTOOL-STATIC Release/sqlite3.a                                                                            
⠴ Building modules: 0/1  CXX(target) Release/obj.target/node_sqlite3/src/backup.o                                                    
⠹ Building modules: 0/1In file included from ../src/backup.cc:2:                                                                     
In file included from /Users/henryoconnor/Documents/secretary/electron-app/src/node_modules/node-addon-api/napi.h:2636:
/Users/henryoconnor/Documents/secretary/electron-app/src/node_modules/node-addon-api/napi-inl.h:1575:24: error: use of undeclared
      identifier 'napi_is_detached_arraybuffer'; did you mean 'napi_is_arraybuffer'?
  napi_status status = napi_is_detached_arraybuffer(_env, _value, &detached);
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
                       napi_is_arraybuffer
/Users/henryoconnor/.electron-gyp/11.3.0/include/node/js_native_api.h:383:25: note: 'napi_is_arraybuffer' declared here
NAPI_EXTERN napi_status napi_is_arraybuffer(napi_env env,
                        ^
In file included from ../src/backup.cc:2:
In file included from /Users/henryoconnor/Documents/secretary/electron-app/src/node_modules/node-addon-api/napi.h:2636:
/Users/henryoconnor/Documents/secretary/electron-app/src/node_modules/node-addon-api/napi-inl.h:1581:24: error: use of undeclared
      identifier 'napi_detach_arraybuffer'
  napi_status status = napi_detach_arraybuffer(_env, _value);
                       ^
⠴ Building modules: 0/1../src/backup.cc:179:9: warning: unused variable 'status' [-Wunused-variable]                                 
    int status = napi_create_async_work(
        ^
../src/backup.cc:252:5: warning: unused variable 'status' [-Wunused-variable]
    BACKUP_BEGIN(Step);
    ^
../src/macros.h:182:9: note: expanded from macro 'BACKUP_BEGIN'
    int status = napi_create_async_work(                                       \
        ^
../src/backup.cc:319:5: warning: unused variable 'status' [-Wunused-variable]
    BACKUP_BEGIN(Finish);
    ^
../src/macros.h:182:9: note: expanded from macro 'BACKUP_BEGIN'
    int status = napi_create_async_work(                                       \
        ^
⠧ Building modules: 0/13 warnings and 2 errors generated.                                                                            
make: *** [Release/obj.target/node_sqlite3/src/backup.o] Error 1
✖ Rebuild Failed                                                                                                                     

An unhandled error occurred inside electron-rebuild
node-gyp failed to rebuild '/Users/henryoconnor/Documents/secretary/electron-app/src/node_modules/sqlite3'.
Error: `make` failed with exit code: 2



Error: node-gyp failed to rebuild '/Users/henryoconnor/Documents/secretary/electron-app/src/node_modules/sqlite3'.
Error: `make` failed with exit code: 2


    at ModuleRebuilder.rebuildNodeGypModule (/Users/henryoconnor/Documents/secretary/electron-app/node_modules/electron-rebuild/lib/src/module-rebuilder.js:193:19)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Rebuilder.rebuildModuleAt (/Users/henryoconnor/Documents/secretary/electron-app/node_modules/electron-rebuild/lib/src/rebuild.js:190:9)
    at async Promise.all (index 105)
    at async Rebuilder.rebuild (/Users/henryoconnor/Documents/secretary/electron-app/node_modules/electron-rebuild/lib/src/rebuild.js:148:13)
    at async /Users/henryoconnor/Documents/secretary/electron-app/node_modules/electron-rebuild/lib/src/cli.js:146:9
child_process.js:655
    throw err;
    ^

Error: Command failed: ../node_modules/.bin/electron-rebuild --parallel --force --types prod,dev,optional --module-dir .
    at checkExecSyncError (child_process.js:616:11)
    at execSync (child_process.js:652:15)
    at Object.<anonymous> (/Users/henryoconnor/Documents/secretary/electron-app/.erb/scripts/ElectronRebuild.js:18:3)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Module._compile (/Users/henryoconnor/Documents/secretary/electron-app/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Object.newLoader [as .js] (/Users/henryoconnor/Documents/secretary/electron-app/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
  status: 255,
  signal: null,
  output: [ null, null, null ],
  pid: 9189,
  stdout: null,
  stderr: null
}
error Command failed with exit code 1.                                                                                               
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.                                                 
error Command failed with exit code 1.                                                                                               
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

package.json

{
  "name": "secretary",
  "productName": "Secretary",
  "description": "A personal secretary for handling daily work.",
  "scripts": {
    "build": "concurrently \"yarn build:main\" \"yarn build:renderer\"",
    "build:main": "cross-env NODE_ENV=production webpack --config ./.erb/configs/webpack.config.main.prod.babel.js",
    "build:renderer": "cross-env NODE_ENV=production webpack --config ./.erb/configs/webpack.config.renderer.prod.babel.js",
    "rebuild": "electron-rebuild --parallel --types prod,dev,optional --module-dir src",
    "lint": "cross-env NODE_ENV=development eslint . --cache --ext .js,.jsx,.ts,.tsx",
    "package": "rm -rf src/dist && yarn build && electron-builder build --publish never",
    "postinstall": "node -r @babel/register .erb/scripts/CheckNativeDep.js && electron-builder install-app-deps && yarn cross-env NODE_ENV=development webpack --config ./.erb/configs/webpack.config.renderer.dev.dll.babel.js && opencollective-postinstall && yarn-deduplicate yarn.lock",
    "start": "node -r @babel/register ./.erb/scripts/CheckPortInUse.js && cross-env yarn start:renderer",
    "start:main": "cross-env NODE_ENV=development electron -r ./.erb/scripts/BabelRegister ./src/main.dev.ts",
    "start:renderer": "cross-env NODE_ENV=development webpack serve --config ./.erb/configs/webpack.config.renderer.dev.babel.js",
    "test": "jest"
  },
  "lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "cross-env NODE_ENV=development eslint --cache"
    ],
    "{*.json,.{babelrc,eslintrc,prettierrc}}": [
      "prettier --ignore-path .eslintignore --parser json --write"
    ],
    "*.{css,scss}": [
      "prettier --ignore-path .eslintignore --single-quote --write"
    ],
    "*.{html,md,yml}": [
      "prettier --ignore-path .eslintignore --single-quote --write"
    ]
  },
  "build": {
    "productName": "Secretary",
    "appId": "secretary",
    "files": [
      "dist/",
      "node_modules/",
      "index.html",
      "main.prod.js",
      "main.prod.js.map",
      "package.json"
    ],
    "afterSign": ".erb/scripts/Notarize.js",
    "mac": {
      "target": [
        "dmg"
      ],
      "type": "distribution",
      "hardenedRuntime": true,
      "entitlements": "assets/entitlements.mac.plist",
      "entitlementsInherit": "assets/entitlements.mac.plist",
      "gatekeeperAssess": false
    },
    "dmg": {
      "contents": [
        {
          "x": 130,
          "y": 220
        },
        {
          "x": 410,
          "y": 220,
          "type": "link",
          "path": "/Applications"
        }
      ]
    },
    "win": {
      "target": [
        "nsis"
      ]
    },
    "linux": {
      "target": [
        "AppImage"
      ],
      "category": "Development"
    },
    "directories": {
      "app": "src",
      "buildResources": "assets",
      "output": "release"
    },
    "extraResources": [
      "./assets/**"
    ],
    "publish": {
      "provider": "github",
      "owner": "electron-react-boilerplate",
      "repo": "electron-react-boilerplate"
    }
  },
  "jest": {
    "testURL": "http://localhost/",
    "moduleNameMapper": {
      "\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/.erb/mocks/fileMock.js",
      "\.(css|less|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "js",
      "jsx",
      "ts",
      "tsx",
      "json"
    ],
    "moduleDirectories": [
      "node_modules",
      "src/node_modules"
    ],
    "setupFiles": [
      "./.erb/scripts/CheckBuildsExist.js"
    ]
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@babel/plugin-proposal-class-properties": "^7.12.1",
    "@babel/plugin-proposal-decorators": "^7.12.1",
    "@babel/plugin-proposal-do-expressions": "^7.12.1",
    "@babel/plugin-proposal-export-default-from": "^7.12.1",
    "@babel/plugin-proposal-export-namespace-from": "^7.12.1",
    "@babel/plugin-proposal-function-bind": "^7.12.1",
    "@babel/plugin-proposal-function-sent": "^7.12.1",
    "@babel/plugin-proposal-json-strings": "^7.12.1",
    "@babel/plugin-proposal-logical-assignment-operators": "^7.12.1",
    "@babel/plugin-proposal-nullish-coalescing-operator": "^7.12.1",
    "@babel/plugin-proposal-optional-chaining": "^7.12.7",
    "@babel/plugin-proposal-pipeline-operator": "^7.12.1",
    "@babel/plugin-proposal-throw-expressions": "^7.12.1",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/plugin-syntax-import-meta": "^7.10.4",
    "@babel/plugin-transform-react-constant-elements": "^7.12.1",
    "@babel/plugin-transform-react-inline-elements": "^7.12.1",
    "@babel/plugin-transform-runtime": "^7.12.1",
    "@babel/preset-env": "^7.12.7",
    "@babel/preset-react": "^7.12.7",
    "@babel/preset-typescript": "^7.12.7",
    "@babel/register": "^7.12.1",
    "@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
    "@teamsupercell/typings-for-css-modules-loader": "^2.4.0",
    "@testing-library/jest-dom": "^5.11.6",
    "@testing-library/react": "^11.2.2",
    "@types/enzyme": "^3.10.5",
    "@types/enzyme-adapter-react-16": "^1.0.6",
    "@types/history": "4.7.6",
    "@types/jest": "^26.0.15",
    "@types/node": "14.14.10",
    "@types/react": "^16.9.44",
    "@types/react-dom": "^16.9.9",
    "@types/react-router-dom": "^5.1.6",
    "@types/react-test-renderer": "^16.9.3",
    "@types/webpack-env": "^1.15.2",
    "@typescript-eslint/eslint-plugin": "^4.8.1",
    "@typescript-eslint/parser": "^4.8.1",
    "babel-eslint": "^10.1.0",
    "babel-jest": "^26.1.0",
    "babel-loader": "^8.2.2",
    "babel-plugin-dev-expression": "^0.2.2",
    "babel-plugin-transform-react-remove-prop-types": "^0.4.24",
    "browserslist-config-erb": "^0.0.1",
    "chalk": "^4.1.0",
    "concurrently": "^5.3.0",
    "core-js": "^3.6.5",
    "cross-env": "^7.0.2",
    "css-loader": "^5.0.1",
    "css-minimizer-webpack-plugin": "^1.1.5",
    "detect-port": "^1.3.0",
    "electron": "^11.0.1",
    "electron-builder": "^22.3.6",
    "electron-devtools-installer": "^3.1.1",
    "electron-notarize": "^1.0.0",
    "electron-rebuild": "^2.3.2",
    "enzyme": "^3.11.0",
    "enzyme-adapter-react-16": "^1.15.3",
    "enzyme-to-json": "^3.5.0",
    "eslint": "^7.5.0",
    "eslint-config-airbnb": "^18.2.0",
    "eslint-config-airbnb-typescript": "^12.0.0",
    "eslint-config-erb": "^2.0.0",
    "eslint-config-prettier": "^6.11.0",
    "eslint-import-resolver-webpack": "^0.13.0",
    "eslint-plugin-compat": "^3.8.0",
    "eslint-plugin-import": "^2.22.0",
    "eslint-plugin-jest": "^24.1.3",
    "eslint-plugin-jsx-a11y": "6.4.1",
    "eslint-plugin-prettier": "^3.1.4",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-react": "^7.20.6",
    "eslint-plugin-react-hooks": "^4.0.8",
    "file-loader": "^6.0.0",
    "husky": "^4.2.5",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^26.1.0",
    "lint-staged": "^10.2.11",
    "mini-css-extract-plugin": "^1.3.1",
    "node-sass": "^5.0.0",
    "opencollective-postinstall": "^2.0.3",
    "prettier": "^2.0.5",
    "react-refresh": "^0.9.0",
    "react-test-renderer": "^17.0.1",
    "rimraf": "^3.0.0",
    "sass-loader": "^10.1.0",
    "style-loader": "^2.0.0",
    "terser-webpack-plugin": "^5.0.3",
    "typescript": "^4.0.5",
    "url-loader": "^4.1.0",
    "webpack": "^5.5.1",
    "webpack-bundle-analyzer": "^4.1.0",
    "webpack-cli": "^4.2.0",
    "webpack-dev-server": "^3.11.0",
    "webpack-merge": "^5.4.0",
    "yarn-deduplicate": "^3.1.0"
  },
  "dependencies": {
    "@fullcalendar/daygrid": "^5.5.0",
    "@fullcalendar/react": "^5.5.0",
    "@getstation/electron-google-oauth2": "^2.1.0",
    "@reduxjs/toolkit": "^1.5.0",
    "@tippyjs/react": "^4.2.0",
    "electron-debug": "^3.1.0",
    "electron-log": "^4.2.4",
    "electron-updater": "^4.3.4",
    "googleapis": "^67.1.0",
    "history": "^5.0.0",
    "jquery": "^3.5.1",
    "js-cookie": "^2.2.1",
    "lodash": "^4.17.21",
    "moment": "^2.29.1",
    "moment-timezone": "^0.5.33",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-modal": "^3.12.1",
    "react-redux": "^7.2.2",
    "react-router-dom": "^5.2.0",
    "redux-devtools-extension": "^2.13.8",
    "redux-thunk": "^2.3.0",
    "regenerator-runtime": "^0.13.7",
    "reselect": "^4.0.0",
    "sanitize-html": "^2.3.2",
    "source-map-support": "^0.5.19",
    "styled-components": "^5.2.1"
  },
  "devEngines": {
    "node": ">=10.x",
    "npm": ">=6.x",
    "yarn": ">=1.21.3"
  },
  "browserslist": [],
  "prettier": {
    "overrides": [
      {
        "files": [
          ".prettierrc",
          ".babelrc",
          ".eslintrc"
        ],
        "options": {
          "parser": "json"
        }
      }
    ],
    "singleQuote": true
  },
  "renovate": {
    "extends": [
      "bliss"
    ],
    "baseBranches": [
      "next"
    ]
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  }
}

src/package.json

{
  "name": "electron-react-boilerplate",
  "productName": "electron-react-boilerplate",
  "version": "2.3.0",
  "description": "Electron application boilerplate based on React, React Router, Webpack, React Hot Loader for rapid application development",
  "main": "./main.prod.js",
  "author": {
    "name": "Electron React Boilerplate Maintainers",
    "email": "electronreactboilerplate@gmail.com",
    "url": "https://github.com/electron-react-boilerplate"
  },
  "scripts": {
    "electron-rebuild": "node -r ../.erb/scripts/BabelRegister.js ../.erb/scripts/ElectronRebuild.js",
    "postinstall": "yarn electron-rebuild"
  },
  "license": "MIT",
  "dependencies": {
    "sqlite": "^4.0.19",
    "sqlite3": "^5.0.2"
  }
}

我将 sqlite3 包降级到 v5.0.0 并正确重建。希望这对遇到同样问题的其他人有所帮助。

来源: