Why can't clang find napi.h header - node-addon-api

Viewed 839

This is my first time writing a nodejs addon and been trying to make node-addon-api package work for hours but I dont get clang to find the napi header file thus i dont get autocompletion in vim and compiler throws a fatal error about it. I've followed node-addon-examples repo but nothing works.

In vim, when including the header file i get this message:

✘   3 #include <napi.h>                                                                                     
    4             'napi.h' file not found                                                                   
    5 int main(in [clang: pp_file_not_found]   

And compiler throws:

cc1: warning: /usr/include/node/node_api.h: not a directory
/home/prxvvy/workspace/a/source/main.c:3:10: fatal error: napi.h: No such file or directory
    3 | #include <napi.h>
      |          ^~~~~~~~

However if I include the node_api header directly like this it does work because, as far as I know when installed nodejs the header files were installed in /usr/include/node but I feel like its not the best way to do the job.

  3 #include <node/node_api.h>                                                                           
  4                                          

My binding.gyp file is:

  1 {                                                                                
  2     "targets": [                                                                                     
  3         {                                                                                            
  4             "target_name": "lib",                                                          
  5             "cflags!": ["-fno-exceptions"],                                                          
  6             "cflags_cc!": ["-fno-exceptions"],                                                       
  7             "cflags": ["-Wall", "-Wno-psabi", "-std=c++14", "-pthread"],         
  8             "cflags_cc": ["-Wall", "-Wno-psabi", "-pthread"],                    
  9             "sources": ["./source/lib.c", "./source/lib.h", "./source/main.c"],  
 10             "include_dirs": [                                                                        
 11                 "<!(node -p \"require('node-addon-api').include_dir\")",         
 12                 "../node_modules/node-addon-api",                                                    
 13                 "node_modules/node-addon-api",                                                       
 14                 "/usr/include/node",                                                                 
 15                 "/usr/local/include/node",                                                           
 16             ],                                                                                       
 17             "dependencies": ["<!(node -p \"require('node-addon-api').gyp\")"],   
 18             "defines": ["NAPI_CPP_EXCEPTIONS"],                                                      
 19         }                                                                                            
 20     ]                                                                                                
 21 }                                                                                                    
~          

I'd like to get some help if im missing something to do

2 Answers

This:

"<!(node -p \"require('node-addon-api').include_dir\")"

Should be:

"<!(node -p \"require('node-addon-api').include\")"

If you launch this in your root package directory:

node -p "require('node-addon-api')"

You should get meaningful output

If you are willing to use CMake.js instead of gyp, then you can add

set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

to your CMakeLists.txt and clang will be able to find the headers.

For example, you can try following these steps:

  1. Clone this example repo
  2. cd node-addon-examples/build_with_cmake/node-addon-api
  3. Add set(CMAKE_EXPORT_COMPILE_COMMANDS 1) to the CMakeLists.txt
  4. npm i
  5. cd build
  6. cmake ..

Now when you open up node-addon-examples/build_with_cmake/node-addon-api/hello.cc, you should not get errors for napi.h or the rest of the file.

Related