NodeJS 从不同的脚本导出多个 类

NodeJS export multiple classes from different scripts

假设我有: script1.js

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

script2.js

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

那我有clients.js

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

最后index.js

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

但是我得到错误:

TypeError: clients.clientA is not a constructor

我是 javascript 的新手,请问您知道我做错了什么吗?

为了能够 require A 和 B,您需要先导出它们:在您的 script1.js 中类似于 module.exports = A,对于 B

也是如此

您没有在“script1”、“script2”中导出任何内容。

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

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