Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to reference node_api.h properly since it was removed? #882

Closed
cinderblock opened this issue Jan 21, 2021 · 3 comments
Closed

How to reference node_api.h properly since it was removed? #882

cinderblock opened this issue Jan 21, 2021 · 3 comments

Comments

@cinderblock
Copy link

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

{
  "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!

@mhdawson
Copy link
Member

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 #855

@richardlau
Copy link
Member

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
$

@mhdawson
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants