Node js server is crashing


I am following this instruction How To Create a Web Server in Node.js with the HTTP Module

When i start node js server. it is working normally. But if i call from chrome browser. Server is crashing.

  root@ubuntu-s-1vcpu-1gb-ams3-01:~/first-servers# node htmlFile.js
Server is running on
    fs.readFile(__dirname + "/index.html")

TypeError: Cannot read property 'readFile' of undefined
    at Server.requestListener (/root/first-servers/htmlFile.js:7:8)
    at emitTwo (events.js:126:13)
    at Server.emit (events.js:214:7)
    at parserOnIncoming (_http_server.js:619:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:115:23)

root@ubuntu-s-1vcpu-1gb-ams3-01:~/first-servers# ls -a
.  ..  csv.js  hello.js  html.js  htmlFile.js  index.html  json.js  routes.js

Thank you.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.


You’re probably missing the fs (NodeJS’s file system module) import, adding this to the first line of your file might work:

const fs = require('fs');

If it complains about the const, you’re probably using an older version of NodeJS, you could either upgrade to a new one or change the const to var.