Nicolas Rey
Nicolas Rey

Reputation: 481

NodeJS export multiple classes from different scripts

Let's say I have: script1.js

class A{
   constructor(){
      this.name = A
   }
}

script2.js

class B {
   constructor(){
      this.name = B
   }
}

then I have clients.js

const clientA = require('./script1');
const clientB = require('./script2');
module.exports = {
   clientA : clientA,
   clientB : clientB
}

And finally index.js

const clients = require('./clients/Clients.js');
const clientA = new clients.clientA();

But I get the error:

TypeError: clients.clientA is not a constructor

I'm kinda new in javascript, do you have any idea what I'm doing wrong here please?

Upvotes: 0

Views: 156

Answers (2)

Omid Navy
Omid Navy

Reputation: 302

You are not exporting anything in "script1","script2".

module.exports = class A{
   constructor(){ 
       this.name = A   
}}

and

module.exports = class B {
   constructor(){
       this.name = B
   }}

Upvotes: 1

Mikhail  Dudin
Mikhail Dudin

Reputation: 494

To be able to require A and B you need to export them first: something like module.exports = A in your script1.js and the same for B

Upvotes: 1

Related Questions