Node.js native addons: where is node_api.h located?
Asked Answered
E

6

23

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.

Ellipsoid answered 11/5, 2020 at 12:57 Comment(0)
E
19

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.

Ellipsoid answered 11/5, 2020 at 13:37 Comment(4)
In my computer it is in C:/Users/<user>/.node-gyp/<version>/include/nodeKeeton
how did you resolve the intellisense without hardcoding your <user> path?Pairs
nice working well, but wondering how to avoid to hardcode the username and the nodeversionMilitary
Side note: On linux, if installed globally with default node settings, they are in: "/usr/include/node"Telegonus
O
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

Orna answered 23/7, 2021 at 8:52 Comment(0)
A
1

I know this is kind of old, but here was my work around to avoid hardcoding. If you aren't using vscode, this probably helps a bit more. I used the below function to generate a compile_flags.txt for clangd After building with node-gyp, there should be a file config.gypi, which is similar to json. there is a property called nodedir which should point to the include path. here's a function that could help find this: This should hopefully be platform independent.


const fsp = require('fs/promises');
const { existsSync, readFileSync } = require('fs');
const assert = require('node:assert');

const findnodeapih = () => {
    assert(existsSync("./build"), "Haven't built the application once yet. Make sure to build it");
    const dir = readFileSync("./build/config.gypi", 'utf8');
    const nodedir_line = dir.match(/"nodedir": "([^"]+)"/);
    assert(nodedir_line, "Found no matches")
    assert(nodedir_line[1]);
    console.log("node_api.h found at: ", nodedir_line[1]);
    return nodedir_line[1]+"/include/node";
};
Alphonso answered 2/11, 2023 at 15:43 Comment(0)
S
1

If you use nvm to manage Node.js, node_api.h is located in ${HOME}/.nvm/versions/node/v16.15.1/include/node.

Spermatic answered 25/1 at 8:7 Comment(0)
P
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

Photomechanical answered 11/5, 2020 at 13:1 Comment(1)
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.Ellipsoid
Q
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..!

Quinquepartite answered 26/12, 2022 at 12:20 Comment(1)
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.Felipa

© 2022 - 2024 — McMap. All rights reserved.