Reputation: 995
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.
Upvotes: 23
Views: 15137
Reputation: 1
More generic, for *nix systems with NVM.
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/node_modules/node-addon-api",
"~/.nvm/versions/node/**"
],
Upvotes: 0
Reputation: 19
If you use nvm to manage Node.js, node_api.h
is located in ${HOME}/.nvm/versions/node/v16.15.1/include/node
.
Upvotes: 1
Reputation: 35
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";
};
Upvotes: 1
Reputation: 6112
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..!
Upvotes: 1
Reputation: 11
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
Upvotes: 1
Reputation: 995
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.
Upvotes: 20
Reputation: 7267
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
Upvotes: 0