0

Building on related issue: Load "Vanilla" Javascript Libraries into Node.js

I'm trying to load jQuery.soap as a NodeJS module. I have followed the steps in the linked issue but I'm having some problems calling the functions. The Soap plugins define:

return $.soap = soap;

To call the function. So I have created an index.js with this code:

var fs = require('fs');

// Read and eval library
filedata = fs.readFileSync('./node_modules/jquery.soap/jquerysoap-  lib/jquery.soap.js','utf8');
eval(filedata);

exports.Soap = $.soap;

Then I'm loading it this way in my nodeJS file.

 var Soap = require('jquery.soap');

and using it this way:

Soap({
    url: myurl,
    appendMethodToURL: false,
    SOAPAction: '',
    ....
)};

And I'm getting a 'undefined is not a function'. Am I exporting correctly? I have tried to put

exports.Soap = soap;

instead of

exports.Soap = $.soap;

But still not working. Once I do the readFileSync should be able to call $.soap? Thanks in advance

Edit: In my VanillaJS module I have something like:

function($){
    function soap (options) {
    }
return $.soap = soap;
 }
Community
  • 1
  • 1
Sergiodiaz53
  • 1,268
  • 2
  • 14
  • 23

1 Answers1

0

You don't import/export correctly. You export 'Soap' as a member of the module, but then import 'Soap' as the entire module in which 'Soap' is only a member.

You should either go:

exports = $.soap;

or:

var Soap = require('jquery.soap').Soap;

BTW call it (lower cased) 'soap'. Don't break conventions.

Alon
  • 10,381
  • 23
  • 88
  • 152
  • I have put 'exports = $.soap;' inside the index.js of my module but the problem persists. Also tried with the other option and still not working. =( – Sergiodiaz53 Oct 14 '16 at 14:31
  • @Sergiodiaz53 have you debbuged "exports.Soap = $.soap;" and verified that $.soap is not undefined? – Alon Oct 14 '16 at 14:40
  • I have updated my question with some info of the structure of my JS library. At that point $.soap works but in my index.js $.soap is undefined. I have tried to put exports.module = soap just before the return $.soap in the library but still 'not defined' – Sergiodiaz53 Oct 17 '16 at 06:08