Search code examples
node.jspathexportnode-modulesthrow

I am new to nodejs and i keep getting this error


I keep getting this error. I have deleted node.js twice and reinstalled again. The same error keeps popping up. error gotten from nodejs

This below is my person.js file which I was trying to export to the index.js file:

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    greeting() {
        console.log(`my name is ${this.name}`)
    }
}

module.exports = Person;

This is the index.js file:

const Person = require("./person")

const person1 = new Person ("Philippa", 30)

person1.greeting()

Also, I tried doing this and i kept getting the same error:

const path = require("path")

console.log(__filename)

console.log(__dirname)

RECENT EDIT: I tried bringing in the path and fs modules since that's what I am working with for now using require(). Still getting this error below:

enter code here
$ node path.js
internal/modules/cjs/loader.js:985
  throw err;
  ^

Error: Cannot find module 'C:\Users\9470M\Documents\node_crash_course\path.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
    at Function.Module._load (internal/modules/cjs/loader.js:864:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Solution

  • You added whitespace to the file person .js. Rename it to person.js