Custom Modules Frequently Asked Questions

When should I use require versus define?

Always use the define Object for entry point scripts or when creating new modules. Use the require Function to load and use existing modules.

There’s a performance advantage to using the require Function. Whenever possible, try to use require. The define Object imports all dependencies up front, while require only loads them as 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 define, you won’t get the performance boost 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 require, you might need to import define first to get the right context. For example, to import a custom module by 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 a global identifier to reference it, and set it up as a dependency. For more examples of importing third-party and non-AMD libraries, see Import a third-party library and Import a third-party JavaScript library.

Related Topics

General Notices