What is difference between require and define in Netsuite SuiteScript 2.x

Anurag KumarAnurag Kumar
1 min read

Require Function

require([dependencies,] callback)

Require Function executes the callback function and loads the dependencies when they are required. On the client, the require function runs asynchronously, and on the server, it runs synchronously.

In require function dependencies are not loaded until they are needed. For example if you included a library as a dependency and it has a method helloworld now when you are calling method from library then only require will load the library dependency and execute the hello world method.

Define Function

define(id, [dependencies,] moduleObject)

In define function all the dependencies are loaded first before the callback function executes.

In define function dependencies are not mandatory, for custom modules you do not need any dependency to pass. In define function both id and dependencies are optional and you do not need to pass only moduleObject is mandatory.

If you are making a entry point script define function must return a key value pair for e.g

define([], function(){

return {

hello: function(){

return "hello";

}

}

});

For suiteScript debugger use require function and not define. Because define doesn't work with debugger.

0
Subscribe to my newsletter

Read articles from Anurag Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anurag Kumar
Anurag Kumar

I am a NetSuite Certified SuiteCloud Developer with over 8 years of work experience.