Javascript Cascade require在NodeJs中,需要一个需要另一个文件NodeJs的文件

Javascript Cascade require在NodeJs中,需要一个需要另一个文件NodeJs的文件,javascript,node.js,ecmascript-6,Javascript,Node.js,Ecmascript 6,我有一份档案。 a.js class A{ constructor(name){ this.name = name; } displayName(){ console.log(this.name); } } module.exports = A; const A = require('./a'); exports.A; const common = require('./common'); var a = new common.A('My na

我有一份档案。 a.js

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

  displayName(){
    console.log(this.name);
  }
}
module.exports = A;
const A = require('./a');
exports.A;
const common  = require('./common');
var a = new common.A('My name is khan and I am not a terrorist');
a.displayName();
另一个文件 common.js

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

  displayName(){
    console.log(this.name);
  }
}
module.exports = A;
const A = require('./a');
exports.A;
const common  = require('./common');
var a = new common.A('My name is khan and I am not a terrorist');
a.displayName();
另一个文件b.js

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

  displayName(){
    console.log(this.name);
  }
}
module.exports = A;
const A = require('./a');
exports.A;
const common  = require('./common');
var a = new common.A('My name is khan and I am not a terrorist');
a.displayName();
我得到一个错误A不是构造函数。 请帮忙,我怎样才能把它做好。
请原谅我的愚蠢错误,我是新手。

以下是你应该做的修复

a.js
文件中,您正在导出
Render
,但是,它应该是
a

class A {
    constructor(name) {
        this.name = name;
    }
    displayName() {
        console.log(this.name);
    }
}
module.exports = A;
common.js
文件中,必须导出由
common
类/函数/变量或其他任何内容组成的
对象,如下所示:

const A = require('./a');
const someOtherVariable = 'Hello World!';
module.exports = {
    A: A,
    someOtherVariable: someOtherVariable,
};
注释:您“必须”的原因是您希望使用具有以下语法的
A
类:
common.A
。。。假设文件名为
common
,您可能会
导出
不止一个
,因此将它们打包到
对象中

最后,在
b.js
文件中,您可以使用
common.A
语法提取要使用的类

const common = require('./common');
const a = new common.A('My name is khan');
a.displayName();
console.log(common.someOtherVariable); // Hello World!

希望这有帮助。

以下是您应该进行的修复

a.js
文件中,您正在导出
Render
,但是,它应该是
a

class A {
    constructor(name) {
        this.name = name;
    }
    displayName() {
        console.log(this.name);
    }
}
module.exports = A;
common.js
文件中,必须导出由
common
类/函数/变量或其他任何内容组成的
对象,如下所示:

const A = require('./a');
const someOtherVariable = 'Hello World!';
module.exports = {
    A: A,
    someOtherVariable: someOtherVariable,
};
注释:您“必须”的原因是您希望使用具有以下语法的
A
类:
common.A
。。。假设文件名为
common
,您可能会
导出
不止一个
,因此将它们打包到
对象中

最后,在
b.js
文件中,您可以使用
common.A
语法提取要使用的类

const common = require('./common');
const a = new common.A('My name is khan');
a.displayName();
console.log(common.someOtherVariable); // Hello World!

希望这能有所帮助。

在a.js中,您应该做的是:module.exports=a,为了在
common.js中保持一致,很抱歉,它的module.exports=a仅适用于。让我来编辑这个问题。@lomboboo添加了module.exports=A;仍然是相同的错误。@Imran请看下面我的答案……在a.js中,您应该执行的操作是:module.exports=a,为了在
common.js中保持一致,module.exports=AI对不起,它的module.exports=a。让我来编辑这个问题。@lomboboo添加了module.exports=A;还是一样的错误。@Imran请看下面我的答案。。。