1
// module.js
module.exports = {
    m1() {
        console.log('m1')
    },
    m2() {
        this.m1()
        console.log('m2')
    }
}
// index.js
const { m2 } = require('./module')
m2()

When I try to access a method from another in a module object, it gives the following error: TypeError: this.m1 is not a function. I have tried this on a normal object dozen of times yet never seen an error like that. Is this an error special to the module object? If so, how can I solve this problem?

  • 1
    No, this happens on any other object whose methods use `this` as well. Use `module.m2()` instead of destructuring, or call `module.exports.m1()` inside `m2` instead of referring to `this`. – Bergi Feb 14 '21 at 13:33

0 Answers0