---
metadata:
  - name: generator
    content: Diplodoc Platform v5.64.2
alternate:
  - https://yandex.com/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/modules.define.md
  - https://yandex.com/dev/jsapi-v2-1/doc/ru/v2-1/ref/reference/modules.define.md
  - href: https://yandex.com/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/modules.define.md
    type: text/markdown
    title: Markdown version
  - href: https://yandex.com/dev/jsapi-v2-1/doc/en/llms.txt?revision=r21950234
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/dev/jsapi-v2-1/doc/en/llms.txt?revision=r21950234

# modules.define

Static function.

Defining a module in the module system.

{% note info %}

We recommend creating your custom modules in your own namespace, to avoid accidentally replacing the modules that are necessary for the API to work.

{% endnote %}

**Returns** self-reference.

```javascript
{ modules } modules.define(module[, depends, resolveCallback[, context]])
```

**Parameters:**

#|
|| **Parameter** | **Default value** | **Description**  ||
|| [`module`](#param-module)[*](*star) | — | Type: String

Module name. ||
|| [`depends`](#param-depends) | — | Type: String[]

Array of names of necessary modules. This argument may be omitted. ||
|| [`resolveCallback`](#param-resolveCallback)[*](*star) | — | Type: Function

Function that defines the module. The first argument in resolveCallback will be a provide function, to which you will need to pass a module. The provide function call can be delayed. Subsequent arguments are the modules specified in the dependencies. The order of the modules will match the order in the depends array. If a module cannot be resolved, the module system must be notified. This can be done by passing a second argument to the provide function. The second argument will be passed to errorCallback and promise as an error in the module request. As a result, the module can be requested again. ||
|| [`context`](#param-context) | — | Type: Object

Context for the function. ||
|#

\* Mandatory parameter/option.

**Examples:**

**1.**

```javascript
// Defining a custom module. 'plugin.*' is a custom namespace.
ymaps.modules.define('CustomModule', function (provide) {
    var CustomModule = function (defValue) {
        this.field = defValue;
    };
    provide(CustomModule);
});
// Requesting modules
ymaps.modules.require(['CustomModule'])
    .spread(
        function (CustomModule) {
            // ...
        },
        this
   );
```

**2.**

```javascript
// Defining a custom asynchronous module.
ymaps.modules.define('CustomAsyncModule', function (provide) {
	// For the module to work, we must load an external script.
    $.getScript( "ajax/test.js")
        .done(function( script, textStatus ) {
            function CustomAsyncModule () {
                // ...
            }
        // Calling the provide function after loading the script.
        provide(CustomAsyncModule);
    });
});
// Requesting modules
ymaps.modules.require(['CustomAsyncModule'])
    .spread(
        function (CustomAsyncModule) {
            // ...
        },
        this
    );
```

**3.**

```javascript
// Creating a custom asynchronous module with consideration for an error.
ymaps.modules.define('plugin.CustomModule', function (provide) {
    $.getScript( "ajax/test.js" )
        .done(function( script, textStatus ) {
            // Processing successful script loading.
            provide({ a: 1 });
        })
        .fail(function( jqxhr, settings, exception ) {
            // Notifying the module system that the module can't be prepared right now.
            provide(null, new Error('Error when loading'));
        });
});

// Requesting modules with error handling.
ymaps.modules.require(['plugin.CustomModule'])
    .spread(
        function (CustomModule) {
            // ... 
        },
        function (error) {
            console.log(error.messsage); // "Error when loading".
            // Code for handling the module loading error. You can request the module one more time.
        },
        this
    );
```

**4.**

```javascript
// Defining a custom module with dependencies.
ymaps.modules.define('CustomLayoutModule', [
     'templateLayoutFactory', 
     'layout.storage'
], function (provide, templateLayoutFactory, layoutStorage) {
    var customLayoutClass = templateLayoutFactory.createClass(
        '<div>My Layout</div>',
        {
            build: function () {
                 customLayoutClass.superclass.build.call(this);
                 // ...
            },
            clear: function () {
                 // ...
                 customLayoutClass.superclass.clear.call(this);
            }
        }
    );
    layoutStorage.add('customLayout', customLayoutClass);
    provide(customLayoutClass);
});
// Requesting a custom layout
ymaps.modules.require(['CustomLayoutModule'])
    .spread(
        function (CustomLayoutModule) {
            // ...
        },
        this
   );
```

[*star]: Mandatory parameter/option.