Dynamic require in RequireJS, getting Module name has not been loaded yet for context error?
The limitation relates to the simplified CommonJS syntax vs. the normal callback syntax:
- http://requirejs.org/docs/whyamd.html#commonjscompat
- https://github.com/jrburke/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define
var foomodule = require('foo');
// do something with fooModule
What's happening behind the scenes is that RequireJS is looking at the body of your function code and parsing out that you need 'foo' and loading it prior to your function execution. However, when a variable or anything other than a simple string, such as your example...var module = require(path); // Call RequireJS require
...then Require is unable to parse this out and automatically convert it. The solution is to convert to the callback syntax;var moduleName = 'foo';
require([moduleName], function(fooModule){
// do something with fooModule
})
Given the above, here is one possible rewrite of your 2nd example to use the standard syntax:define(['dyn_modules'], function (dynModules) {
require(dynModules, function(){
// use arguments since you don't know how many modules you're getting in the callback
for (var i = 0; i < arguments.length; i++){
var mymodule = arguments[i];
// do something with mymodule...
}
});
});
EDIT: From your own answer, I see you're using underscore/lodash, so using _.values
and _.object
can simplify the looping through arguments array as above. Error: Module name @google-cloud/vision has not been loaded yet for context: _. Use require([])
Your code is not compatible with RequireJS which uses AMD - Asynchronous Module Definition.
AMD format looks like this:
define(['@google-cloud/storage'], (storage) {
// body of your module here
});
The question is whether '@google-cloud/storage'
is AMD compatible.The simplest solution here would be to use more modern tooling like webpack or just use native ES6 modules if you support Chrome browser only
Uncaught Error: Module name https has not been loaded yet for context: _. Use require([])
Solution found. Just use browserify. Install browserify and then run the following command-
> browserify Dataverse_API_node.js > bundle.js
Browserify goes through all the 'require' in your script and wraps it up in bundle.js and helps to run the node.js modules in the browser.Just add the following script in your .html file-
<script type=text/javascript src="scripts/bundle.js"></script>
Module name jquery has not been loaded yet for context: _. Use require
You've transpiled the code from TypeScript to CommonJS.
Browsers don't support the CommonJS module system, they only support the ECMAScript module system.
The requirejs
library you use only supports the AMD module system.
You should fix this at the point you transpile the code. You need to target browsers. This is most commonly done using Webpack which has a loader for TypeScript modules.
Aside: If you're
import
ing (and thus bundling) jQuery then you should not also load it from a CDN. Uncaught Error: Module name express has not been loaded yet for context: _
You should read the page that comes attached with the error. express
has not been loaded in your top level require, so you have to change
require(['require']
torequire(['require', 'express']
then you can freely require("express")
in your code. Module name has not been loaded yet for context using RequireJS text plugin
You're trying to require modules using CommonJS syntax which RequireJS tries to emulate by loading the template asynchronously.
Since it's not ready yet when you try to use it, it throws the error.
You'd only need the following to get it to work:
define([
'jquery', 'underscore', 'backbone', 'jquery-ui', 'fancytree',
'text!templates/categories.html'
], function(
$, _, Backbone, ui, fancytree,
tpl
) {
'use strict';
return Backbone.View.extend({
el: $('#tree'),
template: _.template(tpl), // can be put here directly
initialize: function() {
// only has 3 parameters
this.listenTo(this.collection, 'reset add change remove', this.render);
this.collection.fetch();
},
render: function() {
this.$el.html(this.template());
return this;
}
});
});
Additional information (and similar questions):- Dynamic require in RequireJS, getting "Module name has not been loaded yet for context" error?
- Module name has not been loaded yet for context
- Error: Module name "xxx" has not been loaded yet for context
Related Topics
Load "Vanilla" JavaScript Libraries into Node.Js
Using the Haversine Formula in JavaScript
What's "This" in JavaScript Onclick
Efficient Way to Insert a Number into a Sorted Array of Numbers
Adding Code to a JavaScript Function Programmatically
How to Use a Link to Call JavaScript
Why Is Window (And Unsafewindow) Not the Same from a Userscript as from a <Script> Tag
Is Localstorage.Getitem('Item') Better Than Localstorage.Item or Localstorage['Item']
JavaScript Switch VS. If...Else If...Else
Differencebetween Native Objects and Host Objects
How to Upload Preview Image Before Upload Through JavaScript
How to Access Nested JSON Data
Adding a Slide Effect to Bootstrap Dropdown
How to Remove a Table Row with Jquery
How to Calculate the Number of Years Between Two Dates
Access Properties of the Parent with a Handlebars 'Each' Loop
Angular 2.1.0 Create Child Component on the Fly, Dynamically