0

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?

1
  • How have you exported the A and B? Commented Apr 4, 2021 at 11:48

2 Answers 2

1

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

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

1 Comment

Yes. that's right. something like this. ``` module.exports = class A{ constructor(){ this.name = A } } ```
1

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
   }}

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.