node.jshyperledger-fabrichyperledgerchaincode

`peer lifecycle chaincode package`command for Node.js chainode not bundling node_modules


Hyperledger Fabric version: 2.2.4

Fabric Client: Fabric-Tools 2.2 Docker Image - https://hub.docker.com/layers/hyperledger/fabric-tools/2.2/images/sha256-a5f691808466e90942af3f5f72d697cdaa4e545acfc1f645a553986423ef7ea5?context=explore

Chaincode: Fabric-samples Javascript

Context on the issue: I am currently creating a CI/CD pipeline for the deployment of chaincode using the 2.x chaincode lifecycle. I am facing unexpected behavior with Node.js chaincode deployment whereby the node_modules I install using npm install prior to running peer lifecycle chaincode package(per the documentation) are not included in the resulting tar.gz package within code.tar.gz. Because my peer nodes are not able to access the internet to install dependencies, the missing modules result in errors when running the chaincode after installation. More specifically, I get errors related to missing modules like Fabric Shim which are defined in require() statements in the chaincode sample.

When manually deconstructing unpacking the tar.gz structure, the code.tar.gz archive does not include node_modules that are present in the target chaincode directory that I defined the path to in the peer lifecycle chaincode package command.

Expected Behavior: My understanding was that the new chaincode lifecycle packaging process included the dependencies in the tar.gz package before deploying and therefore there is no need to run npm install or otherwise download dependencies from externally at runtime. More specifically, based on the documentation and examples online, it seems the peer lifecycle chaincode package command would include installed dependencies from node_modules in the package for deployment.

Is this not the default behavior of the chaincode packaging mechanism, and if not, is there a best practice for bundling dependencies on Node.js chaincode where npm install at the peer is not possible?

TIA for any help here


Solution

  • The peer lifecycle chaincode package explicitly excludes the node_modules directory. Also as I assume you are currently using the inbuilt docker builder and launcher of the peer, even if you did send a package that contained the node_modules directory, it will still call npm install on it even with the presence of the node_modules directory and may end up trying to interact with an external npm registry.

    I would suggest that you look at either using a chaincode external builder and launcher, see https://hyperledger-fabric.readthedocs.io/en/release-2.2/cc_launcher.html and you could process a chaincode package that already has the node_modules included. A chaincode package is just a tar.gz file so is easy to package yourself as described in the link

    or use chaincode as a service https://hyperledger-fabric.readthedocs.io/en/release-2.2/cc_service.html which doesn't require any code to be packaged and processed by the peer.