How to Create Blockchain in JavaScript

To create a simple Blockchain in Node.js, we need one Genesis Block. You can refer to Genesis Block as the first block of the Blockchain because Blockchain is an array of blocks, and Genesis is the first block of the Blockchain.

Genesis Block

The genesis block is the first block of the Blockchain. The genesis block is generally hardcoded in the applications that utilize its Blockchain. The Genesis Block is also known as Block Zero or Block 0. It is an ancestor of every Blockchain network’s block that can be traced to its origin back. Remember how every block in the Blockchain is linked back to the previous block using the hash in the block header? So you keep returning and realize every block is connected to the genesis block.

Our Genesis Block contains the following fields or properties.

  1. timestamp
  2. lastHash
  3. hash
  4. data

So, if we combine all these properties into one object, it will become a Genesis Block. So based on this block, we will mine the second block. Then we will extract the third block from the second block, and so on. So that is why we need a Genesis Block to start a Blockchain.

Now, let us start a project by creating a project folder and then start building an excellent blockchain using Javascript.

Step 1: Create a project and create a Genesis Data

The first step is to create a project. Type the following command.

mkdir cryptochain 

Go inside that folder.

cd cryptochain

Now, generate the package.json file if we need to install any node modules in the future.

npm init -y

The next step is to create a new file inside the root called genesis.js and add the following code.

const GENESIS_DATA = {
 lastHash: '64b7edc786326651e031a4d12d9838d279571946d8c9a5d448c70db94b0e143f',
 hash: 'c671c84681b9d682b9fd43b2a2ef01a343eab7cfa410df9835f8165007d38467',
 data: 'krunal'

module.exports = { GENESIS_DATA };

Here, we have taken an object with its initial values.

As I have defined earlier, our block contains the four properties. This is genesis data, so we need to hardcode these values.

The properties timestamp and data are you can understand. But I have used the SHA256 algorithm to convert the simple text into hash text.

The actual text for lastHash is a krunal, and the actual text for the hash is a krunalHash.

You can verify it by going to this URL:

Here, you can enter my name as a text, and it will give the SHA256 hash of that text. You must convert that text to lowercase using Javascript’s toLowercase(). That is it. You will find the exact hash like me. Cool!! So now, you have Genesis Data ready. The next thing is to create a Block from this data.

Step 2: Create a Block

Create a file called block.js inside your project root and add the following code.

const { GENESIS_DATA } = require('./genesis.js');

class Block {
  constructor({timestamp, lastHash, hash, data}) {
    this.timestamp = timestamp;
    this.lastHash = lastHash;
    this.hash = hash; = data;

 static genesis() {
   return new this(GENESIS_DATA);

module.exports = Block;

So, here we have imported the GENESIS_DATA for our block.

Then we defined the Block class and passed the parameters to the constructor when we created an object of the Block.

We have also defined the static method called genesis() responsible for returning the Genesis Block for our Blockchain.

Remember, we have previously defined the GENESIS_DATA, not block. Therefore, after creating an object of this class, it will become a Genesis Block.

Step 3: Create a hash based on a previous block.

We must define a function to create a hash based on the previous block’s hash.

So, first, let us create a new file inside the root called crypto-hash.js and add the following code.

// crypto-hash.js

const crypto = require('crypto');

const cryptoHash =(...inputs) => {
 const hash = crypto.createHash('sha256');
 hash.update(inputs.sort().join(' '));
 return hash.digest('hex');

module.exports = cryptoHash;

Now, to create a Hash, we need the three properties.

  1. timestamp
  2. lastHash
  3. data

So, we have first required the crypto module provided by Node.js.

Then we have defined the cryptoHash () function, which will accept the inputs. Here we have used the spread operator, which is the syntax of ES6.

Inside that function, we have called the createHash() method and passed the sha256 as a parameter. That means we need to create a hash based on the sha256 algorithm.

Then we sorted and joined that three parameters and returned their hex values.

So, finally, we can get the current block’s hash based on the previous block’s three properties.

Step 4: Mine a new Block based on a previous Block

So, we got the Genesis Block and the hash of that block. We need to write the function to generate a new Block based on the previous one. Right now, in our case, it is a Genesis Block.

Now, import the crypto-hash module inside the block.js file, create a new function called mineBlock(), and pass the two parameters.

  1. lastBlock
  2. data

So, our final block.js file looks like this.

// block.js

const { GENESIS_DATA } = require('./genesis.js');
const cryptoHash = require('./crypto-hash');

class Block {
 constructor({timestamp, lastHash, hash, data}) {
   this.timestamp = timestamp;
   this.lastHash = lastHash;
   this.hash = hash; = data;

 static genesis() {
   return new this(GENESIS_DATA);

 static mineBlock({lastBlock, data}) {
   const timestamp =;
   const lastHash = lastBlock.hash;
   return new this({
     hash: cryptoHash(timestamp, lastHash, data)
module.exports = Block;

So, we have defined the method called mineBlock and passed the two parameters.

The mineBlock() will return an entirely new Block based on the previous block.

Now, it is time to create a Blockchain from these blocks.

Step 5: Create a Blockchain

We have completed the step of creating a Genesis Block and mining a new Block. Now it is the time to develop a blockchain. That is why create a new file inside the root called blockchain.js and add the following code inside it.

// blockchain.js

const Block = require('./block');

class Blockchain {

 constructor() {
   this.chain = [Block.genesis()];

 addBlock({ data }) {
  const newBlock = Block.mineBlock({
    lastBlock: this.chain[this.chain.length-1],


module.exports = Blockchain;

So, I first imported the Block.js file and then created a class called Blockchain.

The Blockchain class is responsible for adding a new block inside the blockchBlockchainlockchain is an array of the blocks starting with Genesis block.

So in the constructor, we have not defined an empty array. Instead, we have filled the chain array with the Genesis block.

Then we have defined one function called addBlock(), which accepts the data.

Now, before adding a new block to the chain array, we need to mine it. That is why we have called the Block class’s mineBlock() method to extract a new block.

For mining a new block, we need a previous block and data. So that is why we have used this code this.chain[this.chain.length-1] because it will return the last Blockchain to the Blockchain and the data we already pass to that function. So we get the new block based on the previous block and data.

The next step is to add that newly mined Blockchain to our Blockchain, and that is it.

The last step is to run this projeBlockchain the Blockchain.

Step 6: Run the projeBlockchain on the Blockchain.

The last step is to create a file inside the project’s root called server.js and add the following code.

// server.js

const Blockchain = require('./blockchain');
const Block = require('./block');

const blockchain = new Blockchain();

for(let i=0; i<5; i++) {
 const newData = 'krunal'+i;
 blockchain.addBlock({data: newData});


So, here we have imported both the block.js and blockchain.js file and created an object of the Blockchain.

Then we will loop through that Blockchain, add the data, and create the Blockchain because one is Genesis, and five are based on previous blocks.

Go to the terminal and start the node server.

node server

You will see an output like this.

How To Create Blockchain In JavaScript

That’s it.

GitHub Code

2 thoughts on “How to Create Blockchain in JavaScript”

  1. Hi Krunal can you assist me on this error. I just change the data ‘krunal’ using my first name. Thanks in advance. All js are save in one project folder.

    throw err;

    Error: Cannot find module ‘./genesis.js’


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.