为什么导入失败?

时间:2019-07-18 09:58:02

标签: node.js class import require

我的测试环境如下:

index.js

use strict';
const MyUtility   = require('./lib/myutility');
const MyTestClass = require('./lib/mytestclass');

console.log("start new Object");
const testObj = new MyTestClass();
testObj.checkMyUtility();
console.log("end new Object");

MyUtility.js

'use strict';
const MyTestClass = require('./mytestclass');

module.exports = class MyUtility{
   static mylog(msg){
     console.log(msg);
   }
   static getNewObject()
   {

   }
}

和测试班级

mytestclass.js

'use strict';
const MyUtility = require('./myutility');
module.exports =  class MyTestClass{
  constructor()
  {
    console.log("MyTestClassConstructor");
  }
  checkMyUtility()
  {
    console.log(["extern import :",MyUtility ]);// fail {} empty obj
    const MyUtility2 = require('./myutility');
    console.log(["inline import :" ,MyUtility2 ]);//OK MyUtility object
  }
}

在版本1中。我得到测试类的输出:

start new Object
MyTestClassConstructor
[ 'extern import :', {} ] //import fail
[ 'inline import :', [Function: MyUtility] ]
end new Object

如果我删除了require('./ lib / myutility');在index.js中 我得到关注输出

start new Object
MyTestClassConstructor
[ 'extern import :', [Function: MyUtility] ] //ok
[ 'inline import :', [Function: MyUtility] ] //Ok
end new Object

为什么我不能在两个文件中导入实用程序类? 以及我该如何正确执行?

0 个答案:

没有答案