Search code examples
node.jsgoogle-cloud-platformgoogle-cloud-functionsibm-mqn-api

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 people to find it in the future.

It worked fine in the Node v8 engine but it is now deprecated. After upgrading the engine to v10 and bumping some packages (following all migration guides regarding my dependencies) the exception is thrown by the ffi-napi package during yarn install, as follows:

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: ...s status = napi_get_instance_data(env, &d);
                        ^~~~~~~~~~~~~~~~~~~~~~
../src/ffi.cc:11:24: note: suggested alternative: 'napi_new_instance'
   napi_status status = napi_get_instance_data(env, &d);
                        ^~~~~~~~~~~~~~~~~~~~~~
                        napi_new_instance
../src/ffi.cc: In function 'Napi::Object BindingHook(Napi::Env, Napi::Object)':
../src/ffi.cc:347:24: error: 'napi_set_instance_data' was not declared in this scope
   napi_status status = napi_set_instance_data(
                        ^~~~~~~~~~~~~~~~~~~~~~
../src/ffi.cc:347:24: note: suggested alternative: 'napi_new_instance'
   napi_status status = napi_set_instance_data(
                        ^~~~~~~~~~~~~~~~~~~~~~
                        napi_new_instance
In file included from ../src/ffi.cc:3:0:
../src/ffi.h: At global scope:
../src/ffi.h:148:19: warning: mangled name for 'Napi::TypedArray FFI::WrapPointer(Napi::Env, T*, size_t) [with T = char*() throw ()]' will change in C++17 because the exception specification is part of a function type [-Wnoexcept-type]
 inline TypedArray WrapPointer(Env env, T* ptr, size_t length = 0) {
                   ^~~~~~~~~~~
../src/ffi.h:148:19: warning: mangled name for 'Napi::TypedArray FFI::WrapPointer(Napi::Env, T*, size_t) [with T = void*(void*, const char*) throw ()]' will change in C++17 because the exception specification is part of a function type [-Wnoexcept-type]
../src/ffi.h:148:19: warning: mangled name for 'Napi::TypedArray FFI::WrapPointer(Napi::Env, T*, size_t) [with T = int(void*) throw ()]' will change in C++17 because the exception specification is part of a function type [-Wnoexcept-type]
../src/ffi.h:148:19: warning: mangled name for 'Napi::TypedArray FFI::WrapPointer(Napi::Env, T*, size_t) [with T = void*(const char*, int) throw ()]' will change in C++17 because the exception specification is part of a function type [-Wnoexcept-type]
make: *** [Release/obj.target/ffi_bindings/src/ffi.o] Error 1
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:191:23)
gyp ERR! stack     at ChildProcess.emit (events.js:198:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
gyp ERR! System Linux 5.3.0-1020-gcp
gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /workspace/node_modules/ffi-napi
gyp ERR! node -v v10.19.0
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok 
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node-gyp-build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] 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!     /builder/home/.npm/_logs/2020-06-12T17_47_25_766Z-debug.log; Error ID: c4475859

It works fine both locally (OSX Catalina node 10.21) and in Docker (tested in alpine-based node 10 and 12 major tags).


Solution

  • TLDR: It is not supported yet.


    I've just found out some needed Native APIs for the IBM MQ package dependencies (ffi-napi specifically) were introduced in Node v10.20 (napi_get_instance_data added in v10.20.0) but Google Cloud Functions is based on Node v10.18.1.

    The only work around I found so far is to downgrade the ffi-napi to v2.4.7 using Yarn Resolutions or a custom fork. There is no public information regarding an expected release of the Node v10.20 in the Cloud Functions engine.