gpt4 book ai didi

javascript - 当对象是函数时,为什么 module.export 返回未定义?

转载 作者:行者123 更新时间:2023-12-02 21:08:13 24 4
gpt4 key购买 nike

我对 Nodejs 很陌生。我正在使用一个小型图书馆“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 显示日志为:

World undefined

附注“cukefarm”的完整代码位于https://github.com/ReadyTalk/cukefarm

最佳答案

我认为你的安慰是错误的,你有stringified函数而不是对象

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

关于javascript - 当对象是函数时,为什么 module.export 返回未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61167143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com