0

Suppose I have a module, named mainModule.js, that has the statement.

var helper_formatModule = require('/formatModule.js'); 

Inside formatModule.js, I also have a statement,

var helper_handleSentences = require('/handleSentences.js'); 

If my original module, mainModule.js, needs functions defined in the handleSentences.js module would it be able to access them? I.e if it imported formatModule, a module that has handleSentences, does it have access to those? Or would I need to import the handleSentences.js module directly?

Ben Fortune
  • 28,143
  • 10
  • 73
  • 75
Diana
  • 69
  • 1
  • 8
  • `helper_formatModule` generally won't be available in `helper_handleSentences` unless that is what you are exporting. This is due to [how closures work](https://stackoverflow.com/questions/111102/how-do-javascript-closures-work). – Joe Lissner Jul 24 '17 at 15:37

1 Answers1

1

Only requiring a module A somewhere (in module B, for example) doesn't make the functions of A accessible in other modules. Normally, they're not even accessible in module B.

To access functions (or any values) from another module, that other module has to export them. The following scenario will not work:

// module-a.js
function firstFunction () {}
function secondFunction () {}
// module-b.js
var helper_handleSentences = require('/handleSentences.js'); 
// do something with 'helper_handleSentences'
module.exports = function (a) {
  return helper_handleSentences(a);
}

As you can see, module-a.js doesn't export anything. Thus, the variable a holds the default export value, which is an empty object.

In your situation, you can either

1. require both modules in mainModule.js

// handleSentences.js
function doSomethingSecret () {
  // this function can only be accessed in 'handleSentences.js'
}
function handleSentences () {
  // this function can be accessed in any module that requires this module
  doSomethingSecret();
}
module.exports = handleSentences;
// formatModule.js
var helper_handleSentences = require('/handleSentences.js'); 
// do something with 'helper_handleSentences'
module.exports = function (a) {
  return helper_handleSentences(a);
};
// mainModule.js
var helper_handleSentences = require('/handleSentences.js');
var helper_formatModule = require('/formatModule.js'); 
// do something with 'helper_handleSentences' and 'helper_formatModule'

2. merge the exported values of both modules into one object

// handleSentences.js
function doSomethingSecret () {
  // this function can only be accessed in 'handleSentences.js'
}
function handleSentences () {
  // this function can be accessed in any module that requires this module
  doSomethingSecret();
}
module.exports = handleSentences;
// formatModule.js
var helper_handleSentences = require('/handleSentences.js'); 
// do something with 'helper_handleSentences'
function formatModule (a) {
  return helper_handleSentences(a);
};
module.exports = {
  handleSentences: helper_handleSentences,
  format: formatModule
};
// mainModule.js
var helper_formatModule = require('/formatModule.js');
// use both functions as methods
helper_formatModule.handleSentences();
helper_formatModule.format('...');
PeterMader
  • 5,739
  • 1
  • 16
  • 27