Apigee Does Not Understand 'module.exports' Object,How can I export function from JS file to testing file so Apigee Proxy Would Not argue on 'module.export' object

Hello, Community! I have faced an issue. We are making unit-tests and integration tests for proxies. JS is in JS-resource files. When we export function to the test file locally and test them with jest using standard "module.exports" construction, tests run successfully. After we deploy JS-resource files to Edge, making call to proxy gives us error and points problems with 'module' object?

Error:

{ "fault": { "faultstring": "Execution of JS-TransformCompanyResponse failed with error: Javascript runtime error: \"ReferenceError: \"module\" is not defined. (proxy.transform.utils.js:45)\"", "detail": { "errorcode": "steps.javascript.ScriptExecutionFailed" } } }

If there any options to make it in the best possible way? How to make Edge see 'module.exports' or maybe there is another appropriate way to do this? Would be extremely grateful!

,

Solved Solved
1 3 491
1 ACCEPTED SOLUTION

I suppose you need to conditionally export things.

Something like this?

(function (){

  // define things here
   ...


  // EXPORT
  // Handle node.js case
  if ("object" = typeof module && module.exports) {
        module.exports = Whatever;
  }


  // Handle browser case
  else if ( typeof(window) != "undefined" ) {
    window.Whatever  = Whatever;
  }


  // for jrunscript
  else if (typeof define === 'function' && define.amd) {
        define(function () {
            return Whatever;
        });
  } 

  // rhino, etc
  else {
    var globalScope = (function(){ return this; }).call(null);
    globalScope.Whatever = Whatever;
  }


}());

See also, this article.

View solution in original post

3 REPLIES 3

I suppose you need to conditionally export things.

Something like this?

(function (){

  // define things here
   ...


  // EXPORT
  // Handle node.js case
  if ("object" = typeof module && module.exports) {
        module.exports = Whatever;
  }


  // Handle browser case
  else if ( typeof(window) != "undefined" ) {
    window.Whatever  = Whatever;
  }


  // for jrunscript
  else if (typeof define === 'function' && define.amd) {
        define(function () {
            return Whatever;
        });
  } 

  // rhino, etc
  else {
    var globalScope = (function(){ return this; }).call(null);
    globalScope.Whatever = Whatever;
  }


}());

See also, this article.

Thanks a lot, Dino! we have solved this with simple Module object mock:

if (!module) {

var module = { exports: {} }

}

And it works well!

That sounds like a simple, elegant solution.