5

In the walkthrough step 7: JSON Model example, the app apparently works as documented but I see the following error in the console:

Error: Modules that use an anonymous define() call must be loaded with a require() call; they must not be executed via script tag or nested into other modules.

The only other instance of this message that I could find seems, to my untrained eye, to deal with a wholly different scenario.

I've tried both Firefox and Chromium, CDN hosting vs local hosting, two different UI5 versions (1.77.0 and 1.79.0), both minified and plain, so I'd suppose this is really something in the code itself.

What could it be? Also, is it something I can safely ignore and why?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170

1 Answers1

4

Anonymous define

Calling sap.ui.define([...],...) defines a module anonymously because the 1st argument is not a string (module name or module ID) but a list of the module's dependencies. If the module name is omitted, the framework automatically determines it based on how the module script was referenced.

  • Use anonymous sap.ui.define once at top-level of the JS file content, not multiple times.
  • Replace sap.ui.define with sap.ui.require when simply requiring existing modules. See my comment at Issue #2203 ยท SAP/openui5 (github.com).
  • Always address the module with the same registered module ID prefix consistently.* This avoids redundantly registering the same module with different module IDs which, in the anonymous define scenario, would otherwise cause processing the module definition multiple times due the framework determining the module ID from the different ID prefixes.

* Module ID prefixes can be registered with data-sap-ui-resourceroots, sap.ui.loader.config, or formerly with jQuery.sap.registerModulePath.

Named module define

The 1st argument in sap.ui.define("my/demo/Module",[...] ,...) defines the name of the module manually which must be passed when:

  • Defining a nested module within an existing module definition in a single JS file content, which is, however, forbidden according to the sap.ui.define API description:

    A single file must not contain multiple calls to sap.ui.define. [...]

  • Defining a module which was initiated by a <script> tag from HTML.

The walkthrough is fixed with SAP/openui5@6302b8f and SAP/openui5-docs#43 accordingly.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170