Load External Library in Suitescript Server Scripts

0

I’m trying to load sprintf library into my scripts. https://github.com/alexei/sprintf.js/blob/master/src/sprintf.js

I uploaded the library into my file cabinet to SuiteScripts/Modules/sprintf.js

If I load the library in client script, it works fine and as expected as following:

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define([    
    'SuiteScripts/Modules/sprintf.js'],
    function (spf){}
]); 

However, if I do the same steps in any kind of Server scripts such as user event script or suitelet. It doesn’t load at all. and it throws this error: org.mozilla.javascript.EcmaError: TypeError: Cannot call method “sprintf” of undefined

Note: if you use require function in the chrome debugger, it loads and works fine.

Any Suggestion or help would be much appreciated.

Beginner Asked on November 23, 2019 in SuiteScript.
Add Comment
1 Answer(s)
Best answer

Line 217 of sprintf.js is the problem. It makes it so that only environments with the window global defined use the define function. NetSuite’s serverside suitescript 2 environment doesnt define window.  By far the easiest fix is changing sprintf.js so that it doesn’t need window defined to use the define function. If you want to keep sprintf.js intact, you probably need to do something with the shim config.

Advanced Answered on November 23, 2019.

Man,  You are the best, I really reallly appreciate your help and explanation.  Thank you so so much again battk.

on November 23, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Limebox   Become a Sponsor