20

I'm trying to create a native addon for Node.js and when I include

#include <napi.h>

The Intelli Sense of VS Code says that it cannot detect where node_api.h is located (it's included by napi.h).

node-gyp build works well and it compiles. But I do not understand where is that header in the system and where node-gyp gets it from? I need to add the path to the Intelli Sense options and to better understand the process of building in general.

I'm playing with this code example.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
RussCoder
  • 889
  • 1
  • 7
  • 18

4 Answers4

16

I have run a full search on disk C (I'm on Windows 10), and found out that the header file node_api.h is located in

C:\Users\<UserName>\AppData\Local\node-gyp\Cache\<NodeVersion>\include\node

as well as other headers like v8.h.

If you delete that folder, node-gyp build no longer works. node-gyp configure downloads all headers again and restores the above mentioned folder.

RussCoder
  • 889
  • 1
  • 7
  • 18
1

Are you using extension ms-vscode.cpptools by Microsoft? Then you should just add the path for the header files used by napi to your include path in VSCode: Move your cursor over the include line with the error -> chose "Quick Fix" -> there should be an option for setting include path options (exact naming is language specific) -> new tab opens, add the path under "include path"

The header files are located in appdata as described by RussCoder.

Alternatively see: https://code.visualstudio.com/docs/cpp/customize-default-settings-cpp

Ranger
  • 11
  • 2
0

You should take a look at node-addon-api module.

The headers can be included via require('node-addon-api').include or you can find it inside node_modules/node-addon-api folder.

https://github.com/nodejs/node-addon-api/blob/master/doc/setup.md

Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
  • 9
    `require('node-addon-api').include` and `node_modules/node-addon-api` is the same folder as I see in `node_modules/node-addon-api/index.js`. There is `napi.h` but there is no `node_api.h`. – RussCoder May 11 '20 at 13:18
0

On macOS, I found (given an install of Node version 16.17.0) that my node_api.h was stored at ~/.node-gyp/16.17.0/include/node/node_api.h. So I was able to include it via the path ~/.node-gyp/16.17.0/include/**.

So, to get proper Intellisense in VS Code, I edited this config file. Quite a few fields were already set up for me by default, but all I changed with regards to this question was to add an extra path to includePath.

.vscode/c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "~/.node-gyp/16.17.0/include/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}

You can avoid hard-coding the version by changing the path to:

~/.node-gyp/**

... but be warned that if you have multiple versions of node installed, you'll end up including duplicate headers (and having a bad time). So alternatively, you could manually set up a symlink at ~/.node-gyp/current that points to whichever version of node you're using, I guess, and set your path as ~/.node-gyp/current/**. Or just point at one installed version of node arbitrarily and hope that the headers don't change that much between versions..!

Jamie Birch
  • 5,839
  • 1
  • 46
  • 60
  • I ran `npm install -g node-gyp && node-gyp install` on my Homebrew-installed Node 20 on macOS 13.4 but I still don't see a ~/.node-gyp directory. – Boris Verkhovskiy Jun 15 '23 at 02:03