GauravP
GauravP

Reputation: 61

Javascript code is not working in node?

I wrote a code to solve this keyword issues in setTimeout() function. And tried to run it in node and it showed throw err; can't find module error. Then I tried to run it in a browser and it worked. I mean how to know when to test our code in nodejs and when not. This is my code

function person () {
    var firstName ;
    var _this_ = this;

    return {
        saveContext: function(context) {
        _this_ = context;
    },
        setName: function(name) {
        _this_.firstName = name;
    },
        getName: function() {
        console.log(_this_.firstName);
    }
  };
}

var employee1 = new person();
employee1.saveContext(employee1);

employee1.setName('Steve');
employee1.getName();

setTimeout(employee1.getName, 1000);

Upvotes: 1

Views: 105

Answers (2)

Sunil Verma
Sunil Verma

Reputation: 111

Make sure npm port is running using below command to overcome running again and again server.

nodemon entryfilename.js

Secondly , test using npm if installed properly in your root directory.

Upvotes: 0

Bálint
Bálint

Reputation: 4049

The "can't find module" error is thrown when:

  • You try running node on a non-existent file.

  • The file exists, but it's not in that directory.

  • You require a file which doesn't exist.

Upvotes: 3

Related Questions