electron / rebuild

Package to rebuild native Node.js modules against the currently installed Electron version
MIT License
1.01k stars 174 forks source link

Cannot rebuild sqlite3 #546

Open cyrilfr opened 3 years ago

cyrilfr commented 3 years ago

I have an Electron app using SQLite and TypeORM. When I try to rebuild the sqlite3 5.0.0 package for my electron version (7.2.3), I get an error:

electron-rebuild -f -w sqlite3

make: Leaving directory '/home/cyril/app/node_modules/monocypher/build'
⠸ Building module: sqlite3, Completed: 2gyp: Undefined variable napi_build_version in binding.gyp while trying to load binding.gyp
✖ Rebuild Failed

An unhandled error occurred inside electron-rebuild
node-gyp failed to rebuild '/home/cyril/app/node_modules/sqlite3'.
Error: `gyp` failed with exit code: 1

Error: node-gyp failed to rebuild '/home/cyril/app/node_modules/sqlite3'.
Error: `gyp` failed with exit code: 1

    at ModuleRebuilder.rebuildNodeGypModule (/home/cyril/app/node_modules/electron-rebuild/lib/src/module-rebuilder.js:193:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)
gmantri commented 3 years ago

I ran into the same issue when rebuilding sqlite3 on my Windows machine (did not encounter the issue on my Mac). I added the following line of code to the variables section in binding.gyp file and that seems to solve the problem:

"napi_build_version%":"3"

Here's my revised binding.gyp file looks like:

{
  "includes": [ "deps/common-sqlite.gypi" ],
  "variables": {
      "sqlite%":"internal",
      "sqlite_libname%":"sqlite3",
      "napi_build_version%":"3"
  },
  "targets": [
    {
      "target_name": "<(module_name)",
      "cflags!": [ "-fno-exceptions" ],
      "cflags_cc!": [ "-fno-exceptions" ],
      "xcode_settings": { "GCC_ENABLE_CPP_EXCEPTIONS": "YES",
        "CLANG_CXX_LIBRARY": "libc++",
        "MACOSX_DEPLOYMENT_TARGET": "10.7",
      },
      "msvs_settings": {
        "VCCLCompilerTool": { "ExceptionHandling": 1 },
      },
      "include_dirs": [
        "<!@(node -p \"require('node-addon-api').include\")"],
      "conditions": [
        ["sqlite != 'internal'", {
            "include_dirs": [
              "<!@(node -p \"require('node-addon-api').include\")", "<(sqlite)/include" ],
            "libraries": [
               "-l<(sqlite_libname)"
            ],
            "conditions": [ [ "OS=='linux'", {"libraries+":["-Wl,-rpath=<@(sqlite)/lib"]} ] ],
            "conditions": [ [ "OS!='win'", {"libraries+":["-L<@(sqlite)/lib"]} ] ],
            'msvs_settings': {
              'VCLinkerTool': {
                'AdditionalLibraryDirectories': [
                  '<(sqlite)/lib'
                ],
              },
            }
        },
        {
            "dependencies": [
              "<!(node -p \"require('node-addon-api').gyp\")",
              "deps/sqlite3.gyp:sqlite3"
            ]
        }
        ]
      ],
      "sources": [
        "src/backup.cc",
        "src/database.cc",
        "src/node_sqlite3.cc",
        "src/statement.cc"
      ],
      "defines": [ "NAPI_VERSION=<(napi_build_version)", "NAPI_DISABLE_CPP_EXCEPTIONS=1" ]
    },
    {
      "target_name": "action_after_build",
      "type": "none",
      "dependencies": [ "<(module_name)" ],
      "copies": [
          {
            "files": [ "<(PRODUCT_DIR)/<(module_name).node" ],
            "destination": "<(module_path)"
          }
      ]
    }
  ]
}

However I still have this issue where the folder name contains {napi_build_version} string.

Hope this helps.