Questions tagged [n-api]

N-API (pronounced N as in the letter, followed by API) is an API for building native Addons. It is independent from the underlying JavaScript runtime (for example, V8) and is maintained as part of Node.js itself. This API will be Application Binary Interface (ABI) stable across versions of Node.js. It is intended to insulate Addons from changes in the underlying JavaScript engine and allow modules compiled for one major version to run on later major versions of Node.js without recompilation.

For more information refer to the official reference: https://nodejs.org/api/n-api.html

61 questions
9
votes
2 answers

Call an existing JavaScript function from a C++ Node.js addon - from another thread

Maybe I just don't search for the right terms but I'm stuck.. I need to call a JavaScript function from C++, very similar to what can be done using the plain C API. Note: I don't want to pass a callback to the C++ code but I already know the name of…
frans
  • 8,868
  • 11
  • 58
  • 132
8
votes
1 answer

How to use node-addon-api's AsyncContext asynchronously

The docs for AsyncContext have this example (only slightly modified, but still working) that works synchronously: #include "napi.h" void MakeCallbackWithAsyncContext(const Napi::CallbackInfo& info) { Napi::Function callback =…
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
7
votes
2 answers

How to return a C++ Class to NODE.JS

Setup: I have a NODE.JS application that must perform some computation at low latency I decided to use N-API and node-gyp to include a native C++ module to the NODE.JS application Current Status: I got to the point where the toolchain works, I can…
7
votes
4 answers

How to use napi_threadsafe_function for NodeJS Native Addon

I've been looking through the NAPI documentation to try and understand how it deals with multithreading. According to the documentation napi_create_threadsafe_function() and napi_call_threadsafe_function() are used to create and call js functions…
Frederik Petersen
  • 1,025
  • 3
  • 16
  • 30
7
votes
1 answer

Streaming data into a Node.js C++ Addon with N-API

I am building a C++ addon for NodeJS and I want to stream data asynchronously from C++ to Node. I have found this article, https://nodeaddons.com/streaming-data-into-a-node-js-c-addon/, however; I want to use the N-API instead of NAN. I have been…
Philip Nelson
  • 1,027
  • 12
  • 28
6
votes
3 answers

How can you call an emitter callback from separate c++ thread in an addon?

For context, I started with this question. I need to call the callback for the emitter in another thread. I made a minimal example but it segfaults on emit.Call({cb, result}); My first instinct is that I have a problem with the lifetimes of env or…
Philip Nelson
  • 1,027
  • 12
  • 28
5
votes
1 answer

How to create async function using NAPI that return Promises

i am trying to create node module using NAPI .I have to create async function that returns promises. I don't want that testasynfunction will block NodeJS event loop. do_something_asynchronous is a synchronous function. napi_deferred…
Shivam Kumar
  • 123
  • 3
  • 10
4
votes
4 answers

Error "Cannot read property 'indexOf' of undefined" binding n-api module

I wrote a c++ module using n-api, compiled it with cmake-js and now want to use it in my electron-vue app. If I use the module in a project without electron-vue it works. But when I try to use it in my electron-vue app I'm always getting this…
groboter
  • 81
  • 2
  • 6
4
votes
1 answer

Calling a C(++) function from Node.JS using N-API

I want to use a C-Function from Node.js by using N-API with node-addon-api module wrapper. This is the first time with N-API for me and I'm also a beginner with Node and C++. I have a experience in C programming of embedded systems but this Node.jS…
groboter
  • 81
  • 2
  • 6
4
votes
1 answer

How do I return a BigInt type from C++ to javascript using N-API?

I'm using this https://github.com/nodejs/node-addon-api/blob/master/doc/bigint.md document as a reference to return a bigint from c++ but I'm getting the following error: error: ‘BigInt’ in namespace ‘Napi’ does not name a type Napi::BigInt…
Ameet Gohil
  • 93
  • 1
  • 6
4
votes
1 answer

How do I save a callback for later with node-addon-api?

I want my C library to be able to call a JS function multiple times. I got it to work using Nan but am having trouble converting it to N-API/node-addon-api. How do I save a JS callback function and call it later from C? Here's what I have using…
iffy
  • 719
  • 1
  • 4
  • 20
3
votes
2 answers

How to resolve Node.js Promise in N-Api Addon C

My main problem is to call the asynchronous function from Node.js in addon and get the returned value. I am trying to resolve the promise returned from the called JS function. index.js const addon = require('./build/Debug/addon.node'); async…
3
votes
1 answer

How to use ffi-napi in Google Cloud Functions at Node v10

I'm experiencing problems with native modules dependencies (specifically IBM MQ for js) inside a Google Cloud Function (Node v10) and didn't found anywhere the information I needed, so I've opened this question and answered myself to allow other…
3
votes
1 answer

Unable to run a simple N-API hello world example

am trying to run this following post in Node.js v12.13.0 stack but unable to render it https://medium.com/@tarkus/how-to-call-c-c-code-from-node-js-86a773033892 [ec2-user@sandbox cpptest]$ sudo npm install npm WARN lifecycle cpptest@1.0.0~install:…
Node User
  • 31
  • 3
3
votes
1 answer

NodeJS, difference between N-API, nan, node-gyp and cmake-js

I am trying to learn how to use C and/or C++ libraries inside NodeJS. But it is somewhat confusing the many different tools and approaches, and the relation between them. What is the difference between NodeJS N-API and "nan", and what is the role…
Paulo Coghi
  • 13,724
  • 14
  • 68
  • 90
1
2 3 4 5