Custom Module Guidelines

Choosing Between the define Object and require Function

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

The require Function can provide a performance advantage because it loads dependencies as needed. The define Object imports all dependencies up front.

The following table summarizes when to use the define Object and the 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   
  }); 
}); 

                    

Using Third-Party Libraries with Custom Modules

You can use third-party libraries by defining the library as a custom module, setting a global identifier to reference it, and setting it up as a dependency. For more examples of using third-party and non-AMD-compatible libraries, see Use an AMD-Compatible Library and Use a Third-Party JavaScript Library.

Related Topics

General Notices