How do you debug a Node.js addon built with CMake.js?
Asked Answered
C

2

5

I want to be able to step though my C++ code for a Node.js addon that I am making. I understand that CMake.js has the --debug option, but there is no documentation about it.

I am using the node-addon-api module, in CLion.

Cheju answered 27/7, 2018 at 5:27 Comment(1)
I just found that cmake-js build -debug stays in Release, but cmake-js rebuild -debug rebuilds with debug.Towering
C
5

After months of blind debugging though the use of Errors to print variables, I have finally figured out how to attach the CLion debugger to a Node.js addon.

  1. Create a new CMake Application configuration.
  2. Fill in these fields:
    • Target: Your project
    • Executable: The Node binary (On Unix run which node or where.exe node on Windows to get the path)
    • Program arguments: Path to your JS file
    • Working directory: The directory where the JS file is located
    • Before launch: Build
  3. Start this configuration in debug mode.
Cheju answered 21/9, 2018 at 5:33 Comment(3)
I am curious to know what you have set in the Target field of your debugger. This seems really promising as I am also tired of using prints in my node addon.Tangled
@Tangled If you click the down symbol in the target field to open the menu, does your project name appear? If not you'll have to troubleshoot it because the project name should be there.Cheju
How do you add NAPI includes and js libs so that it builds with Clion CMAKE?Radiology
P
3

I recently stumbled on the same problem and had success creating a custom toolchain in CLion 2020.3 with CMake.js on a Linux system.

Reproducible steps:

  1. Install cmake-js via npm install -g cmake-js. Make sure to install the package globally, so that your toolchain becomes available across multiple projects.
  2. Create a npm project, e.g mkdir my-project && cd my-project && npm init.
  3. Run npm install bindings && npm install node-addon-api (For the C++ wrapper).
  4. Create a CMakeLists.txt in the root directory and paste this. Make sure to replace file(GLOB SOURCE_FILES hello.cpp) with your addon-specific cpp and header files.
  5. Open my-project in CLion.
  6. Go to Settings / Preferences | Build, Execution, Deployment | Custom Build Targets and click + to add a new target.
  7. Go to Build | Tool Settings | Program and set it to the cmake-js binary that you downloaded in the npm directory where you keep your global packages.
  8. Set the arguments to compile -D and set the working directory to the root directory of my-project.
  9. Go to Clean | Tool Settings | Program and set it to the directory where you keep your cmake-js binary. Set the arguments to clean and set the working directory to your project's root directory.
  10. Add a new Run Configuration and specify the Toolchain you just created in the Target field. Point the Executable to your node executable and add the .js file where you import your native addons to. Set the working directory to the current directory as well. Now, you can build the target, and also debug the N-API layer of your native code, too!
Pursuant answered 14/12, 2020 at 19:23 Comment(5)
Could you make a demo video please? I do what you said and my breakpoint not working.Tumblebug
Could you be more specific? At which step exactly are you encountering problems? Send me a picture of your run configuration, as well as your toolchain configuration.Pursuant
it works! wrong configuration, my mistake. thx!Tumblebug
At step 10, you need to create a Run Configuration from the template Custom Build Application.Barrow
You also need to disable Before Launch > Build because cmake-js is going to build for us instead of CLion.Barrow

© 2022 - 2024 — McMap. All rights reserved.