Search code examples
javascriptnode.jsecmascript-6es6-moduleses6-class

Exporting Class with Require yields error


There is a nuance that I seem to be missing. I have Googled so many fixes recommended for this problem I don't know what is real anymore, nothing seems to work.

MyProject on Replit and the offending class I am trying to export.

I keep getting the same error. I have commented out other changes I have tried in my effort. I want to cut the Gordian knot and just paste the ReplitDB class into my MoodDB class, but I also want to understand what I am missing here.

Offending lines:

ReplitDB.js - Method A

...
exports.module = ReplitDB;

ReplitDB.js - Method B

modules.export = ReplitDB ;

returns:

ReferenceError: modules is not defined
    at Object.<anonymous> (/home/runner/Class-Extends-Error/libs/ReplitDB.js:110:1)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/home/runner/Class-Extends-Error/libs/MoodDB.js:23:18)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)

MoodDB.js:

const ReplitDB = require('./ReplitDB.js')

class MoodDB extends ReplitDB {
    constructor(){
        super()
    }
    
    ...
}

Error:

TypeError: Class extends value #<Object> is not a constructor or null
    at Object.<anonymous> (/home/runner/Class-Extends-Error/libs/MoodDB.js:7:22)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/home/runner/Class-Extends-Error/index.js:12:16)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)

Solution

  • There is a mistake in your export. It should be

    module.exports = ReplitDB;