Custom Modules Frequently Asked Questions

When should I use require versus define?

Always use the define Object in entry point scripts and when creating new modules. Use the require Function for loading and using existing modules.

There is a performance advantage to using the require Function. Generally, give preference to using the require Function whenever you can. The define Object will imports all dependencies. The require Function loads dependencies only as they are needed.

Here is a summary of when to use or not use the define Object and require Function:

Object/Function

When to use

Caveat

define Object

  • To define entry point scripts

  • To create new modules

  • To export modules

  • To import modules using a relative path

If you use the define Object, you do not receive the performance benefit of progressive loading.

require Function

  • To import modules using an absolute path (relative path requires context)

  • To step through code in the SuiteScript debugger

  • To support building a test framework

  • For progressive loading of dependencies

If you use the require Function, you may need to import a define that provides correct context. For example, to import a custom module with a relative path:

                        define(['require'], function(require){
   require(['./ParentFolder/customFile'], function(customFile){        
     //your code   
  }); 
}); 

                      

Are third-party libraries supported?

Yes. You can define the library as a custom module, set up a global identifier to reference it, and configure it as a dependency. See Import a third-party library and Import a third-party JavaScript library.

Related Topics

General Notices