0

I'm pretty new to node js, however I have the js code below:

var fs = require("fs");
var text = fs.readFileSync('data.txt').toString().split("\n");
var i = 0;
var c = "";
var d = "";
var t = "";
var r = "";

for(i = 0;i<text.length;i++){
    if(i == 0){
        c = text[i];
        document.getElementById('con').innerHTML = c;
    } else if(i == 1){
        d = text[i]
        document.getElementById('dec').innerHTML = d;
    } else if(i == 2){
        t = text[i]
        document.getElementById('tes').innerHTML = t;
    } else if(i == 3){
        r = text[i]
        document.getElementById('rec').innerHTML = r;
    }
}

Please note text.length = 4 always.

In Inspect Element, I get the error --

Uncaught ReferenceError: require is not defined

-- on line 1.

What is happening here?

  • You can't run nodejs code in the browser. – tkausl Apr 18 '20 at 15:33
  • 3
    Does this answer your question? [Client on node: Uncaught ReferenceError: require is not defined](https://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined) – BadPiggie Apr 18 '20 at 15:35
  • `require()` is not available in `browser/client-side` Javascript – BadPiggie Apr 18 '20 at 15:35
  • 1
    Node.js is not a language, it's a JavaScript engine. It's a piece of software you need to download and install in your computer: https://nodejs.org/ – Álvaro González Apr 18 '20 at 15:39
  • The link from @BagPiggie is the answer you look for. Not sure if this should be marked as duplicated but that is definitely the solution. – davidmpaz Apr 18 '20 at 15:40

0 Answers0