Node-addon-api: How to reference node_api.h properly since it was removed?

Created on 21 Jan 2021  路  3Comments  路  Source: nodejs/node-addon-api

I apologize if this is not the right place for this but I'm not sure where the best place to start is.

Since #643 removed external-napi/node_api.h from published versions, what is the recommended way to have IntelliSense (and other code linters/hinters) "see" that file (or, really, the right one)?

This is a similar problem for the node-addon-api header files that is usually solved with something like this: node -p "require('node-addon-api').include". For my purposes, I've noticed that this path is always ${workspaceFolder}/node_modules/node-addon-api so I'm using that for now.

I've been looking for a way to consistently setup developers' environments to point to the real node_api.h that's currently in use. Specifically, I'm interested in setting up VS Code for this problem with .vscode/c_cpp_properties.json#configurations[0].includePath.

This SO question has part of an answer: https://stackoverflow.com/questions/61730307/node-js-native-addons-where-is-node-api-h-located

However it's fundamentally flawed (at least for my purposes) in two ways:

  • It references an absolute, OS specific, location
  • Untrivial to parameterize around node version

I've tried things like npm i node to see if the locally installed version has the header file that I can reference. Alas the binary distribution of Node doesn't include the node_api.h header.

My current best solution is to instruct developers to put the path for their system that matches the SO answer into their local user configuration files and then having something like this in the committed .vscode/c_cpp_properties.json:

{
  "configurations": [
    {
      "name": "gcc-node",
      "includePath": [
        "${workspaceFolder}/node_modules/node-addon-api",
        "${config:local.node-gyp.node-cache}/include/node"
      ]
    }
  ],
  "version": 4
}

User Settings
settings.json { "local.node-gyp.node-cache": "C:/Users/<UserName>/AppData/Local/node-gyp/Cache/<NodeVersion>" }

But I feel like there should be something better than this...

Thanks for the look! Cheers!

Most helpful comment

This SO question has part of an answer: https://stackoverflow.com/questions/61730307/node-js-native-addons-where-is-node-api-h-located

The location referenced in the answers there are reliant on node-gyp having already run and downloaded and unpacked the headers tarball (e.g. in the process of installing the module).

I've tried things like npm i node to see if the locally installed version has the header file that I can reference. Alas the binary distribution of Node doesn't include the node_api.h header.

Binary distributions from nodejs.org do contain the node_api.h except on Windows. I don't know the reason for that discrepancy.

In terms of the headers the best place currently is to get it from the node headers tarball, for example: https://nodejs.org/dist/v14.15.4/node-v14.15.4-headers.tar.xz. This is what is used when addons are built when they are installed through npm.

For releases from nodejs.org the URL to the matching headers tarball is in process.release.headersUrl:
e.g.

$  node -p process.release.headersUrl
https://nodejs.org/download/release/v14.15.4/node-v14.15.4-headers.tar.gz
$

All 3 comments

In terms of the headers the best place currently is to get it from the node headers tarball, for example: https://nodejs.org/dist/v14.15.4/node-v14.15.4-headers.tar.xz. This is what is used when addons are built when they are installed through npm.

We are also discussing the possibility of distributing it separately through an npm in https://github.com/nodejs/node-addon-api/issues/855

This SO question has part of an answer: https://stackoverflow.com/questions/61730307/node-js-native-addons-where-is-node-api-h-located

The location referenced in the answers there are reliant on node-gyp having already run and downloaded and unpacked the headers tarball (e.g. in the process of installing the module).

I've tried things like npm i node to see if the locally installed version has the header file that I can reference. Alas the binary distribution of Node doesn't include the node_api.h header.

Binary distributions from nodejs.org do contain the node_api.h except on Windows. I don't know the reason for that discrepancy.

In terms of the headers the best place currently is to get it from the node headers tarball, for example: https://nodejs.org/dist/v14.15.4/node-v14.15.4-headers.tar.xz. This is what is used when addons are built when they are installed through npm.

For releases from nodejs.org the URL to the matching headers tarball is in process.release.headersUrl:
e.g.

$  node -p process.release.headersUrl
https://nodejs.org/download/release/v14.15.4/node-v14.15.4-headers.tar.gz
$

@cinderblock just wondering if that answered your question and this can be closed?

Was this page helpful?
0 / 5 - 0 ratings