cmake-js: Electron build doesn't work properly

Hi, not sure if I’m doing something wrong on my end but when I try to build my Electron C++ addon with CMake.js I end up with some weird issues. The biggest one is that the app instatnly crashes when I call args[0]->IsString() during function argument validation. There doesn’t seem to be a problem with the other validation methods like IsNumber() or IsStringObject(). After I toggled on the --enable-logging flag in Electron I was able to figure out that this is the error Electron crashes on: [13508:1016/202149.533:ERROR:crashpad_client_win.cc(799)] not connected.

Another weird behavior is that when I’m logging something with std::cout it only actually gets printed when compiling in Debug mode. Neither of these issues appeared when I tried building with node-gyp.

My CMakeLists.txt file is just the very basic configuration:

cmake_minimum_required (VERSION 3.5)
set(CMAKE_CXX_STANDARD 17)
project(test)

include_directories(${CMAKE_JS_INC})
add_library(${PROJECT_NAME} SHARED main.cpp ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries (${PROJECT_NAME} ${CMAKE_JS_LIB})

and this is my setup in package.json:

"cmake-js": {
    "runtime": "electron",
    "runtimeVersion": "10.1.3",
    "arch": "x64"
}

Am I missing something or is this some sort of internal CMake.js issues? Thanks.

About this issue

Most upvoted comments

For Electron 9+ V8_31BIT_SMIS_ON_64BIT_ARCH should be defined.

For Electron 9+ for arm64 and for x64 V8_COMPRESS_POINTERS should also be defined.

For Electron 11+ V8_REVERSE_JSARGS should also be defined.

Since Electron 9 it uses the patch to enable pointer compression in v8 and its node.js: https://github.com/electron/electron/pull/21468/files

Now all native node modules should be built with the V8_COMPRESS_POINTERS macro defined only on 64-bit platforms and the V8_31BIT_SMIS_ON_64BIT_ARCH macro defined on all platforms (which seems counterintuitive, but in the patch this is defined by default)

Those macros don’t affect node-addon-abi, that is why it works fine.

cc @unbornchikken @mastergberry

For Electron 9+ V8_31BIT_SMIS_ON_64BIT_ARCH should be defined.

For Electron 9+ for arm64 and for x64 V8_COMPRESS_POINTERS should also be defined.

For Electron 11+ V8_REVERSE_JSARGS should also be defined.

After several days of trying to run my native module, built with cmake-js, in electron, this finally was the breakthrough. Can this be pinned somewhere ?

And thanks! @vadim-termius

I actually migrated my project today from nan -> node-addon-api and suddenly saw all of my problems disappear.

I attempted this morning to find the difference between a cmake-js and node-gyp module, but I didn’t have any success, even after manually overriding most of the options in the .vcxproj file.

Maybe @unbornchikken can take a look at why cmake-js/cmake no longer work with electron 9+

If this can’t be resolved soonish we will probably be forced to go back to node-gyp as we need other fixes from higher versions of electron and 8.5.2 won’t be supported forever.