{"id":791,"hash":"f896fa15e6cbef6e4edb0ebca8ca5957c435120831601e132870edf69097ffeb","pattern":"Uncaught TypeError: this.method is not a function - Node js class export","full_message":"I am new to node.js and I am trying to require a class. I have used https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes as reference. However, when I do this for example:\n\n// talker.js\nclass Talker {\n    talk(msg) {\n        console.log(this.say(msg))\n        var t = setTimeout(this.talk, 5000, 'hello again');\n    }\n    say(msg) {\n        return msg\n    }\n}\nexport default Talker\n\n// app.js\nimport Talker from './taker.js'\nconst talker = new Talker()\ntalker.talk('hello')\n\nI get: \n\n  talker.js:4 Uncaught TypeError: this.say is not a function\n\nIt should be said that app.js is the electron.js renderer process and it bundled using rollup.js\n\nAny ideas why this would be?\n\nUpdate: Sorry, I forgot to add in a line when putting in the psuedo code. It actually happens when I call setTimeout with callback. I have updated the code.","ecosystem":"npm","package_name":"node.js","package_version":null,"solution":"You are losing the bind of this to your method.\n\nChange from this:\n\nsetTimeout(this.talk, 5000, 'hello again');\n\nto this:\n\nsetTimeout(this.talk.bind(this), 5000, 'hello again');\n\nWhen you pass this.talk as a function argument, it takes this and looks up the method talk and passes a reference to that function.  But, it only passes a reference to that function.  There is no longer any association with the object you had in this.  .bind() allows you to pass a reference to a tiny stub function that will keep track of this and call your method as this.say(), not just as say().\n\nYou can see the same thing if you just did this:\n\nconst talker = new Talker();'\n\nconst fn = talker.say;\nfn();\n\nThis would generate the same issue because assigning the method to fn takes no associate to talker with it at all.  It's just a function reference without any association with an object.  In fact:\n\ntalker.say === Talker.prototype.say\n\nWhat .bind() does is create a small stub function that will save the object value and will then call your method using that object.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/42238984/uncaught-typeerror-this-method-is-not-a-function-node-js-class-export","votes":10,"created_at":"2026-04-19T04:51:43.005937+00:00","updated_at":"2026-04-19T04:51:43.005937+00:00"}