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

编程入门 行业动态 更新时间:2024-10-08 18:40:10

当对象是<a href=https://www.elefans.com/category/jswz/34/1771370.html style=函数时,为什么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返回未定义?

本文发布于:2024-05-07 03:50:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754222.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   未定义   module   export

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!