user1559625
user1559625

Reputation: 2673

Why is module.export return undefined when the object is a function?

I am quite new to nodejs. I am using a small library 'cukefarm'. And the file structure looks like below.

index.js
lib/support/World.js
lib/protractor.conf.js

index.js looks like:

World = require('./lib/support/World');
config = require('./lib/protractor.conf');

module.exports = {
  World: World,
  config: config
}

World.js looks like:

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 looks like:

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, "  "));

I try to console log the exports object in both World.js and protractor.conf.js.

protractor.conf works fine, it prints the object no problem.

But World shows log as:

World undefined

P.S. the complete code of 'cukefarm' is located at https://github.com/ReadyTalk/cukefarm

Upvotes: 0

Views: 230

Answers (1)

xdeepakv
xdeepakv

Reputation: 8125

I think you are consoled wrong, You had stringified function not the 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")
}

Upvotes: 1

Related Questions