0

I am trying to create an IBM Cloud Functions blockchain node.js action that uses a Hyperledger Fabric node SDK package, but I'm having trouble requiring the fabric-network package in the action.

Since it is a 3rd party package, it seems I need to upload the action as a zipped file, but when I do that, I see:

"error": "Initialization has failed due to: Error: Failed to load gRPC binary module because it was not installed for the current system\nExpected directory: node-v57-linux-x64-glibc\nFound: [node-v57-darwin-x64-unknown]\nThis problem can often be fixed by running \"npm rebuild\" on the current system"

I want to create a javascript action like the following:

'use strict'

const { X509WalletMixin, Gateway } = require('fabric-network')

async function main(params) {
  return { message: 'success' }
}

What is the correct way to handle 3rd party packages like this?

ccorley
  • 28
  • 2

1 Answers1

3

Node.js modules with native dependencies need to be compiled for the same platform architecture as the serverless runtime. If you are bundling the node_modules directory from your local development machine, it probably won't match.

There are two approaches to use libraries with native dependencies...

  1. Run npm install inside a Docker container from the platform images.
  2. Building custom runtime image with libraries pre-installed.

The first approach is easiest but can only be used when a zip file containing all source files and libraries is less than the action size limit (48MB).

Running npm install inside runtime container

  • Run the following command to bind the local directory into the runtime container and run npm install.
docker run -it -v $PWD:/nodejsAction openwhisk/action-nodejs-v10 "npm install"

This will leave a node_modules folder with native dependencies compiled for correct runtime.

  • Zip up the action source files including node_modules directory.
zip -r action.zip *
  • Create new action with action archive.
ibmcloud wsk action create my-action --kind nodejs:10 action.zip

Building custom runtime image

  • Create a Dockerfile with the npm install command run during build.
FROM openwhisk/action-nodejs-v10

RUN npm install fabric-network
  • Build and push the image to Docker Hub.
$ docker build -t <USERNAME>/custom-runtime .
$ docker push <USERNAME>/custom-runtime
  • Create new action using custom runtime image.
ibmcloud wsk action create my-action --docker <USERNAME>/custom-runtime action.zip

Make sure the node_modules included in the action.zip does not include the same libraries files.

James Thomas
  • 4,303
  • 1
  • 20
  • 26
  • I would recommend to not include any node_modules in action.zip when doing a custom runtime image, just use package.json and install all npm packages inside the image, the image gets cached then action.zip only your your files with no dependencies – csantanapr Apr 18 '19 at 12:41