skip to Main Content

i am currently learning node js
and I am getting

ReferenceError: readFile is not defined

after typing node . in the terminal

here is the code


const express = require("express");

const app = express();

app.get("/", (request, response) => {
  readFile("./home.html", "utf8", (err, html) => {
    if (err) {
      response.status(500).send("sorry, out of order");
    }

    response.send(html);
  });
});

app.listen(process.env.PORT || 3000, () =>
  console.log("http://localhost:3000")
);


2

Answers


  1. You need to import file system module:

    const fs = require('fs');
    // ...
    
    fs.readFile('...')
    
    Login or Signup to reply.
  2. In nodeJS if you want to do operation(Read, Write etc.) on files you have to use file system module provided by nodeJS.
    Before using this module you have to import(ES6) or require it.
    You can take reference from below how to use file system in node js

    const fs = require('fs')
    fs.readFile('Demo.txt', 'utf8', function(err, data){
      
    // Display the file content
    console.log(data);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search