1

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);
5
  • 2
    Works for me in both Node 4 and Node 6. I wouldn't, at all, recommend writing code like that, but it works. Commented Nov 6, 2016 at 17:58
  • 1
    "Or how can I improve my code. " is too broad for SO. You might review the guidelines over at codereview and, if this is in keeping with them, post there. Commented Nov 6, 2016 at 18:00
  • 1
    Please post the full error and the full relevant source code. We can't help you debug what we can't see. Commented Nov 6, 2016 at 18:21
  • Yes, it works now! Thanks, I was just practising to solve the issue. And I will keep that in mind :) @T.J.Crowder Commented Nov 6, 2016 at 18:25
  • Sorry for it. Edited my question now @T.J.Crowder Commented Nov 6, 2016 at 18:32

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! :) The file was up a directory from workspace.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.