1

I'm setting up a blockchain client-server network with NodeJS and ExpressJS. I have the server running successfully and the blockchain functioning by itself. But I'm having some problems combining the two. Firstly, here is my project structure:

node_p2p/
  node_modules/
    ...
  public/
    block.js
    client.js
    index.html
  package-lock.json
  package.json
  server.js

The actual problem that I have is, I want to include block.js in the client.js file, but I always get ReferenceError: require is not defined on the line, where I include block.js into client.js:

var block = require('./block.js');

I have already tried the following variations, but all of them produce the same error (quite expected, since require directory is relative to the file calling require, but I tried them anyway, just in case):

var block = require('../public/block.js');
var block = require('../public/block');
var block = require('./public/block.js');
var block = require('./public/block');
var block = require('/public/block.js');
var block = require('/public/block');
var block = require('../block.js');
var block = require('../block');
var block = require('./block.js');
var block = require('./block');
var block = require('block.js');
var block = require('block');

Here's the code of block.js:

const SHA256 = require ('crypto-js/sha256');

class Block{
   constructor(...){...}
   ...
}

class Blockhain{
   constructor(...){...}
   ...
}

module.exports = {
  Blockchain: function(){
    return new Blockchain();
  }
}

Secondly, client.js:

var socket = io.connect('http://localhost:3000');
var block = require('./block.js');

socket.on('client', showData);
socket.on('clientno', showData);
socket.on('newclient', showData);
function showData(data) {
  console.log(data);
}

var nascoin = new block.Blockchain();

And finally, server.js:

var express = require('express');

var app = express();
var server = app.listen(3000);

app.use(express.static('public'));

console.log("server running!");

var socket = require('socket.io');
var io = socket(server);

io.sockets.on('connection', newConnection);

var client_counter = 0;

function newConnection(socket) {
  console.log('new conn: ' + socket.id);
  client_counter++;

  socket.emit('client', 'your client name: ' + socket.id);
  socket.emit('clientno', 'you are client no.: ' + client_counter);
  socket.broadcast.emit('newclient', 'new client has joined the fray');
  socket.on('server', function(data) {
    console.log(data);
  });
}

I don't even know what else I can do, either I'm just that dumb or that blind...

sparrow
  • 13
  • 2
  • It seems your `client.js` is run inside a browser, so you might want to look into [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) or use something like [babel](https://babeljs.io/). `require()` is a NodeJS function and not included in all JS environments. – Sirko Apr 17 '19 at 09:56

2 Answers2

1

The error you get,

ReferenceError: require is not defined

has nothing to do with the actual module you're trying to load not being available. It means the function require isn't available at all, just like you'd be calling any old function that's not defined.

> require('./pkdxfjdghbdhfksdh')
Error: Cannot find module './pkdxfjdghbdhfksdh'

would be what you'd get (in Node) if require can't find a module.

How exactly are you running this code? If you're actually running it in Node, e.g. node client.js, this error shouldn't occur. If you're attempting to run things in the browser, you will need a bundler like Webpack or Parcel.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • I'm running my server with ```node server.js```, but I have the ```public/client.js``` included in ```public/index.html```, which has been running just fine up until I added the ```var block = require('./block.js');``` line. – sparrow Apr 17 '19 at 10:02
  • Right. `require` does not exist in browsers – you'll need a bundler. https://parceljs.org/ is arguably the easiest to get started with. – AKX Apr 17 '19 at 10:05
0

It seems, you use client.js, which attached to index.html, just in browser. I recommend you to bundle you client.js code with any bundler, webpack for example. It will bild browser-compatible code, that you can use in index.html. Browsers haven't require built in implementations.

dgtlfx
  • 101
  • 1
  • 7