6712

No dependencies defined when they are loaded via absolute paths

Question:

I have some module like this:

define('hello',[], function() { return { say: function(word) { console.log("Hello, "+word) }, }; });

And I'm using it like this (without any require.config) :

require(["hello"], function(hello) { console.log("main",hello); hello.say("main"); });

So far, so good.

But when I'm trying to require the same module with an absolute path, I've got my dependence module undefined:

require(["http://example.com/js/hello.js"], function(hello) { console.log("main",hello); hello.say("main"); });

Console:

main undefined Uncaught TypeError: Cannot read property 'say' of undefined // Oops!

Why is it so?

Answer1:

Named module (define("NAME", [ ... ], function() { ... })) is meant to be required under exact that name. When it is required with a URL it is loaded correctly, but registers itself under its "desired" name, after which requirejs loses track of it still looking for a module with a name http://example.com/js/hello.js.

The reason the name given to define() isn't overriden with the one under which the module was required is to allow multiple module definitions to coexist in the file, for example after optimization. Optimizer will convert all define calls to the form with explicit name.

The reason the absolute name isn't converted to a module id is that this conversion is impossible. All the configuration options of requirejs determine how to convert module id to script location, not other way around.

<a href="http://requirejs.org/docs/api.html#modulename" rel="nofollow">Documentation discourages use of the named modules</a>:

<blockquote>

These are normally generated by the optimization tool. You can explicitly name modules yourself, but it makes the modules less portable ... It is normally best to avoid coding in a name for the module and just let the optimization tool burn in the module names...

</blockquote>

Anonymous module, i.e.:

<pre class="lang-js prettyprint-override">define([], function() { return { say: function(word) { console.log("Hello, "+word) }, }; });

works with either module id ("hello") or absolute path, because it is first registered without a name and later receives the name under which it was required.

Recommend

  • Ruby: Setting select model value not working via AngularJS binding
  • jQuery. Apply selector to every field in a dynamic form
  • How to use a subversion repository as a template for new projects?
  • Detect if WM_MOUSEMOVE is caused by touch/pen
  • How do I get colored output to a file from a bash script running in crontab?
  • Linking Play Developer Console to Google Wallet Error
  • Test if Wireless Adapter is Working Before Resetting
  • Implementing a timer(countdown) that runs pararell with the game logic [duplicate]
  • Basic Slot Machine
  • Laravel loses order when querying
  • Fantom-text in jTextField. How?
  • Round up to nearest power of 10
  • Mapping a list to a Huffman Tree whilst preserving relative order
  • How to set which Boost unit test to run
  • Re-fill posted form data via PHP securely
  • jQuery file download plugin
  • Can one add a complex type item to ListModel?
  • Understanding Intl.DateTimeFormat as a JavaScript object
  • JSON Error when parsing “… has no method 'replace'”
  • Android application not restoring state when installed from .apk, works fine from eclipse
  • Randomizing -and remembering that randomisation- multiple choice questions in php
  • Python cosine function precision [duplicate]
  • HttpClient: disabling chunked encoding
  • What does “t” refer to in this SQL?
  • CERN ROOT exporting data to plain text
  • Trying to get the char code of ENTER key
  • calculate gradient output for Theta update rule
  • Does Mobilefirst provide a provision to access web services directly?
  • Django simple Captcha “No module named fields” error
  • Why does access(2) check for real and not effective UID?
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Getting last autonumber in access
  • Is there a amazon webstore API for customers?
  • How to get a value (ex: baseURL) in every Karate feature?
  • Do create extension work in single-user mode in postgres?
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • Checking variable from a different class in C#
  • Does armcc optimizes non-volatile variables with -O0?