require([dependencies,] callback)

Method Description

Function used to load a module only when the module is needed. When NetSuite executes the require function, it executes the callback function specified in the require function and loads the dependencies when they are required.

If you add a module as a dependency and the module is never used, the dependency is never loaded.

Note:

This function conforms to the Asynchronous Module Definition (AMD) specification. For more information, see require Function.

Returns

void

Global object

require Function

Since

2015.2

Parameters

Parameter

Type

Required / Optional

Description

Since

dependencies

string []

optional

Represents all module dependencies required by the callback function.

Use the following syntax:

  • Native SuiteScript 2.x modules: [‘N/<module name>’]

  • Custom modules: [‘/<path to module file in File Cabinet>/<module name>’]

    See Module Dependency Paths.

2015.2

callback

Function

required

Callback function to execute. Dependent modules are not loaded until they are required.

2015.2

Errors

Error Code

Message

Thrown If

MODULE_DOES_NOT_EXIST

Module does not exist: {module path/name}

The NetSuite module or custom module dependency does not exist.

Note that NetSuite only reports the first error encountered. If you have multiple dependent modules and you receive this error, verify that all module paths and names are correct.

Syntax

The following example shows progressive loading of modules in a script using the require function.

          define({
     newInstance: function (type)
     {
          switch (type)
          {
               case 'lib1' :
                    require(['/lib1'], function (lib1) // Module Loader loads lib1
                    {
                         return new lib1();
                    })
               break;
               case 'lib2' :
                    require(['/lib2'], function (lib2) // Module Loader loads lib2
                    {
                         return new lib2();
                    })
                    break;
               default :
                    return null;
          }
     }
}); 

        

Related Topics

General Notices