Working on creating an add-on that will return an Object
to the node environment. Basing my work on Atul Anand's introduction to N-API in C++, the methods of the class object are wrapped in InstanceMethod()
to expose them; but that function wants a method that returns a Napi::Value
. I can't figure out the invocation to convert the C++ pointer into a Value
; Value::From(env, ptr)
errors (Visual C++ 2017) with "cannot convert from 'initializer list' to 'Napi::Value'".

- 15,848
- 18
- 99
- 158

- 1,224
- 10
- 26
2 Answers
It seems that even as I was asking here, someone else was asking at the Node-API-Addon github site. The solution initially posted to the user's question was what I needed to get my code working.
In short (and obvious in retrospect): the C++ pointer is useless in JavaScript, the method needs to return a JavaScript object wrapping the C++ object. The JS object is maintained within a napi_ref
(Napi::Reference
) and the reference's Value()
is what gets returned from the access method.

- 1,224
- 10
- 26
You will have to use napi_wrap and napi_unwrap in your native code during cross boundary object access.
napi_unwrap(): to retrieves a native instance that was previously wrapped in a JavaScript object
napi_wrap(): to wraps a native instance in a JavaScript object
The following URL is for n-api documentation Object Wrap, that has more detailed information about it. https://nodejs.org/api/n-api.html#n_api_object_wrap
The following github example has this usage
https://github.com/nodejs/node-addon-examples/tree/master/8_passing_wrapped/napi

- 1,346
- 8
- 15
-
Not so useful: If you['d read the link I posted, you'd see I am using the N-API C++ wrapper classes, which hide the `napi_XXX` API calls, and *that* is the problem I wanted to solve. Not really interested in having to dig deeper, but I had to anyway because N-API is so scantly documented. – Mike C May 22 '19 at 17:15