Search code examples
node.jshyperledger-fabrichyperledgerhyperledger-chaincode

Hyperledger Fabric / Nodejs - what's the difference between running a contract with node and fabric-chaincode-node


When browsing the fabric-samples repository that contains examples in Node.js, I noticed that some chaincodes use fabric-chaincode-node while other chaincodes only use node when executing the npm start command:

with fabric-chaincode-node example:

"name": "fabcar",
"version": "1.0.0",
"description": "FabCar contract implemented in JavaScript",
"main": "index.js",
"engines": {
   "node": ">=12",
   "npm": ">=6.9"
},
"scripts": {
   "lint": "eslint .",
   "pretest": "npm run lint",
   "test": "nyc mocha --recursive",
   "start": "fabric-chaincode-node start"
},
"engineStrict": true,
"author": "Hyperledger",
"license": "Apache-2.0",
"dependencies": {
   "fabric-contract-api": "^2.0.0",
   "fabric-shim": "^2.0.0"
}

with node only example:

{
    "name": "abstore",
    "version": "1.0.0",
    "description": "ABstore chaincode implemented in node.js",
    "engines": {
        "node": ">=8.4.0",
        "npm": ">=5.3.0"
    },
    "scripts": {
        "start": "node abstore.js"
    },
    "engine-strict": true,
    "license": "Apache-2.0",
    "dependencies": {
        "fabric-shim": "^2.0.0"
    }
}

I would like to know what is the difference between running npm start command using fabric-chaincode-node and node. Depends on Hyperledger Fabric version? It depends on what packages I'm using, i.e. fabric-shim only needs node, while fabric-contract-api needs fabric-chaincode-node to run correctly?


Solution

  • Samples starting the chaincode with node abstore.js will be older samples using the lower-level fabric-shim API directly to implement the smart contract. The code being run will include a line similar to shim.start(new Chaincode()); to programmatically launch the chaincode.

    Samples starting the chaincode with fabric-chaincode-node start are more current samples using the newer fabric-contract-api API to implement the smart contract. This API is based on and extends the fabric-shim API, and is the recommended approach.

    There is a more detailed description of how to use the current APIs here:

    https://github.com/hyperledger/fabric-chaincode-node/blob/main/TUTORIAL.md