1

When using windows the Napi and I run npm i it compiles and works just fine. When doing the same on linux I get this error with the napi-inl.h

project/node_modules/node-addon-api/napi-inl.h: In instantiation of ‘static Napi::Function Napi::Function::New(napi_env, Callable, const char*, void*) [with Callable = Napi::Promise (*)(Napi::CallbackInfo&); napi_env = napi_env__*]’:
../test.cc:10:50:   required from here
project/node_modules/node-addon-api/napi-inl.h:1985:22: error: cannot bind non-const lvalue reference of type ‘Napi::CallbackInfo&’ to an rvalue of type ‘Napi::CallbackInfo’
 1985 |   typedef decltype(cb(CallbackInfo(nullptr, nullptr))) ReturnType;
      |                    ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
project/node_modules/node-addon-api/napi-inl.h:1985:22: error: cannot bind non-const lvalue reference of type ‘Napi::CallbackInfo&’ to an rvalue of type ‘Napi::CallbackInfo’
project/node_modules/node-addon-api/napi-inl.h:1996:5: error: type ‘<type error>’ argument given to ‘delete’, expected pointer
 1996 |     delete callbackData;
      |     ^~~~~~

test.cc looks like this

#include <napi.h>
#include "processData.h"


// This is were the Node module function name is assigned
// when imported getData() will be the function to call
// It calls Process()
Napi::Object Init(Napi::Env env, Napi::Object exports) {
  exports.Set(Napi::String::New(env, "getData"),
              Napi::Function::New(env, Process));
  return exports;
}

NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)

I don't know if this is an issue with me or with NAPI

F22lightning
  • 620
  • 6
  • 16
  • What is your `node.js` and `node-addon-api` version ? Do you build with N-API v3+ ? (i guess your linux build problem is related to [this merge](https://github.com/nodejs/node-addon-api/pull/395)) – Zilog80 May 21 '21 at 12:45
  • Im using node v14 and the @latest node-addon-api, for this guarded callback what do I have to do? I don't really understand that. – F22lightning May 21 '21 at 18:14

2 Answers2

1

Discovered that the problem was a simple mistake involving the function declaration in Process.cc missing the const keyword

old way:

Napi::Promise Process(Napi::CallbackInfo& info)

new way:

Napi::Promise Process(const Napi::CallbackInfo& info)
F22lightning
  • 620
  • 6
  • 16
0

It's because npm i are different on both Windows and Linux. Quoting an answer to another question:

Yes, there can be differences, say, if you (or your dependencies) use native node.js addons, which are built e.g. by node-gyp and contain native binary code. Also there can be OS/CPU - specific stuff in package.json. package.json description can be found here: https://docs.npmjs.com/files/package.json

  • The problem isn't with npm i as it is doing exactly what I expect it to do. The problem occurs with napi headers and not wanting to compile on linux but having no problem on windows. – F22lightning May 18 '21 at 14:46
  • @F22lightning Huh, you copied the file from Windows straight to Linux, right? I feel like some of the headers are different on Linux. – Caio Francisco May 21 '21 at 20:38
  • I wrote a program using Node addon api (NAPI) that is installed with NPM and is supposed to be cross platform, my c code is pretty simple but the compiler isn’t failing at that point but instead at the NAPI part – F22lightning May 23 '21 at 21:01
  • @F22lightning As far as I know, NAPI is a native node.js addon, right? Then I think it really is the reason why NAPI is failing to compile. – Caio Francisco May 24 '21 at 01:03