函数时,为什么module.export返回未定义?"/>
当对象是函数时,为什么module.export返回未定义?
我对Node.js很陌生。我正在使用小型图书馆'cukefarm'。文件结构如下所示。
index.js
lib/support/World.js
lib/protractor.conf.js
index.js看起来像:
World = require('./lib/support/World');
config = require('./lib/protractor.conf');
module.exports = {
World: World,
config: config
}
World.js看起来像:
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
function World() {
this.Q = require('q');
this.currentPage = null;
this.pageObjectMap = null;
chai.use(chaiAsPromised);
this.expect = chai.expect;
};
module.exports = World;
console.log('\n' + __filename);
console.log('World ' + JSON.stringify(World, null, " "));
protractor.conf.js看起来像:
var path = require('path');
module.exports = {
framework: 'custom',
frameworkPath: require.resolve('protractor-cucumber-framework'),
capabilities: {
'chromeOptions': {
args: ['--test-type']
}
},
cucumberOpts: {
require: [
path.resolve('./node_modules/cukefarm/lib/step_definitions/GeneralStepDefs'),
path.resolve('./node_modules/cukefarm/lib/support/Transform'),
path.resolve('./node_modules/cukefarm/lib/support/World')
],
format: []
},
onPrepare: function() {
browser.ignoreSynchronization = true;
browser.manage().timeouts().setScriptTimeout(5000);
return browser.manage().timeouts().implicitlyWait(5000);
}
};
console.log('\n' + __filename);
console.log('module.exports ' + JSON.stringify(module.exports, null, " "));
我尝试在World.js和protractor.conf.js中控制台记录导出对象。
protractor.conf可以正常工作,它可以毫无问题地打印对象。
但是World显示日志为:
世界未定义
任何人都可以帮助解释吗?
P.S。 “ cukefarm”的完整代码位于
回答如下:我认为您的控制台操作有误,您具有stringified
功能而不是object
const World = require("./env");
console.log(JSON.stringify(World, null, 4)); // undefined
console.log("World " + JSON.stringify(World, null, " ")); // World undefined
console.log(JSON.stringify(new World(), null, 4)); // test
// env.js
module.exports = function() {
console.log("test")
}
更多推荐
当对象是函数时,为什么module.export返回未定义?
发布评论