3

我试图在 Node.js v12.13.0 堆栈中运行以下帖子,但无法呈现它 https://medium.com/@tarkus/how-to-call-cc-code-from-node-js-86a773033892

[ec2-user@sandbox cpptest]$ sudo npm install
npm WARN lifecycle cpptest@1.0.0~install: cannot run in wd cpptest@1.0.0 node-gyp rebuild (wd=/home/ec2-user/cpptest)
npm WARN cpptest@1.0.0 No description
npm WARN cpptest@1.0.0 No repository field.

audited 1 package in 0.577s
found 0 vulnerabilities

[ec2-user@sandbox cpptest]$ node --napi-modules -e \
>   "console.log(require('./build/Release/native.node').hello())"
internal/modules/cjs/loader.js:797
    throw err;
    ^

Error: Cannot find module './build/Release/native.node'
Require stack:
- /home/ec2-user/cpptest/[eval]
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15)
    at Function.Module._load (internal/modules/cjs/loader.js:687:27)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at [eval]:1:13
    at Script.runInThisContext (vm.js:116:20)
    at Object.runInThisContext (vm.js:306:38)
    at Object.<anonymous> ([eval]-wrapper:9:26)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at evalScript (internal/process/execution.js:80:25) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/ec2-user/cpptest/[eval]' ]
}

如果我以普通用户身份运行,这就是我得到的

[ec2-user@sandbox cpptest]$ npm install

> cpptest@1.0.0 install /home/ec2-user/cpptest
> node-gyp rebuild

make: Entering directory `/home/ec2-user/cpptest/build'
  SOLINK_MODULE(target) Release/obj.target/node_modules/node-addon-api/src/nothing.node
  COPY Release/nothing.node
  CXX(target) Release/obj.target/native/binding.o
In file included from /home/ec2-user/cpptest/node_modules/node-addon-api/napi.h:1449:0,
                 from ../binding.cpp:1:
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h: In member function ‘Napi::Value Napi::Function::MakeCallback(napi_value, size_t, napi_value__* const*) const’:
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h:1277:44: error: cannot convert ‘napi_value’ to ‘napi_async_context’ for argument ‘2’ to ‘napi_status napi_make_callback(napi_env, napi_async_context, napi_value, napi_value, size_t, napi_value__* const*, napi_value__**)’
     _env, recv, _value, argc, args, &result);
                                            ^
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h: In static member function ‘static void Napi::Error::Fatal(const char*, const char*)’:
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h:1484:37: error: invalid conversion from ‘const char*’ to ‘size_t {aka long unsigned int}’ [-fpermissive]
   napi_fatal_error(location, message);
                                     ^
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h:1484:37: error: too few arguments to function ‘void napi_fatal_error(const char*, size_t, const char*, size_t)’
In file included from /home/ec2-user/cpptest/node_modules/node-addon-api/napi.h:4:0,
                 from ../binding.cpp:1:
/home/ec2-user/.cache/node-gyp/12.13.0/include/node/node_api.h:98:33: note: declared here
 NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location,
                                 ^
In file included from /home/ec2-user/cpptest/node_modules/node-addon-api/napi.h:1449:0,
                 from ../binding.cpp:1:
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h: In member function ‘bool Napi::CallbackInfo::IsConstructCall() const’:
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h:2020:76: error: ‘napi_is_construct_call’ was not declared in this scope
   napi_status status = napi_is_construct_call(_env, _info, &isConstructCall);
                                                                            ^
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h: In static member function ‘static napi_value__* Napi::ObjectWrap<T>::ConstructorCallbackWrapper(napi_env, napi_callback_info)’:
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h:2474:74: error: there are no arguments to ‘napi_is_construct_call’ that depend on a template parameter, so a declaration of ‘napi_is_construct_call’ must be available [-fpermissive]
   napi_status status = napi_is_construct_call(env, info, &isConstructCall);
                                                                          ^
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h:2474:74: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h: In constructor ‘Napi::AsyncWorker::AsyncWorker(const Napi::Object&, const Napi::Function&)’:
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h:2684:50: error: cannot convert ‘void (*)(napi_env, void*) {aka void (*)(napi_env__*, void*)}’ to ‘napi_value’ for argument ‘2’ to ‘napi_status napi_create_async_work(napi_env, napi_value, napi_value, napi_async_execute_callback, napi_async_complete_callback, void*, napi_async_work__**)’
     _env, OnExecute, OnWorkComplete, this, &_work);
                                                  ^
In file included from /home/ec2-user/cpptest/node_modules/node-addon-api/napi.h:4:0,
                 from ../binding.cpp:1:
../binding.cpp: At global scope:
/home/ec2-user/.cache/node-gyp/12.13.0/include/node/node_api.h:66:5: error: invalid conversion from ‘void (*)(napi_env, napi_value, napi_value, void*) {aka void (*)(napi_env__*, napi_value__*, napi_value__*, void*)}’ to ‘napi_addon_register_func {aka napi_value__* (*)(napi_env__*, napi_value__*)}’ [-fpermissive]
     };                                                                \
     ^
/home/ec2-user/.cache/node-gyp/12.13.0/include/node/node_api.h:73:3: note: in expansion of macro ‘NAPI_MODULE_X’
   NAPI_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
   ^
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h:164:3: note: in expansion of macro ‘NAPI_MODULE’
   NAPI_MODULE(modname, __napi_ ## regfunc);
   ^
../binding.cpp:9:1: note: in expansion of macro ‘NODE_API_MODULE’
 NODE_API_MODULE(addon, Init)
 ^
In file included from /home/ec2-user/cpptest/node_modules/node-addon-api/napi.h:1449:0,
                 from ../binding.cpp:1:
/home/ec2-user/cpptest/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::String (*)(const Napi::CallbackInfo&); napi_env = napi_env__*]’:
../binding.cpp:7:48:   required from here
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h:1212:57: error: invalid conversion from ‘napi_value__* (*)(napi_env, napi_callback_info) {aka napi_value__* (*)(napi_env__*, napi_callback_info__*)}’ to ‘size_t {aka long unsigned int}’ [-fpermissive]
     env, utf8name, CbData::Wrapper, callbackData, &value);
                                                         ^
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h:1212:57: error: cannot convert ‘Napi::details::CallbackData<Napi::String (*)(const Napi::CallbackInfo&), Napi::String>*’ to ‘napi_callback {aka napi_value__* (*)(napi_env__*, napi_callback_info__*)}’ for argument ‘4’ to ‘napi_status napi_create_function(napi_env, const char*, size_t, napi_callback, void*, napi_value__**)’
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h: In static member function ‘static void Napi::Error::Fatal(const char*, const char*)’:
/home/ec2-user/cpptest/node_modules/node-addon-api/napi-inl.h:1485:1: warning: ‘noreturn’ function does return [enabled by default]
 }
 ^
make: *** [Release/obj.target/native/binding.o] Error 1
make: Leaving directory `/home/ec2-user/cpptest/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (events.js:210:5)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
gyp ERR! System Linux 3.10.0-514.26.2.el7.x86_64
gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/ec2-user/cpptest
gyp ERR! node -v v12.13.0
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok 
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! cpptest@1.0.0 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the cpptest@1.0.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ec2-user/.npm/_logs/2019-11-13T08_04_56_318Z-debug.log

它似乎只是给了我们 make fail 错误,我们无法弄清楚。

任何建议或帮助将不胜感激。

4

1 回答 1

1

似乎问题在于您使用的节点 12.x 没有节点插件 API,但它有 n-api https://nodejs.org/docs/latest-v12.x/ ​​api/

文章参考Node Addon API https://nodejs.org/api/n-api.html (与nodejs 12.x节点版本中的n-api不同)

适用于n-api的示例参考https://github.com/nodejs/node-addon-examples

于 2021-06-08T10:58:18.480 回答