'define' is not defined error on RequireJS & Webapp Yo generator
You load your scripts manually here and here, rendering the whole point of requireJS
useless. You also load main
first here config.js#L49.
Instead, you should only have this line in your index.html
<script data-main="scripts/config" src="scripts/vendor/requirejs/require.js"></script>
And load all your dependencies in that file (like you do with main
) using define()
and require()
. As you have set exports
, which sets the values as globals, the functions can be empty. Here's an sample:
define([
"jquery",
"underscore",
"backbone",
"marionette",
"modernizr"
], function () {
require([
"backbone.babysitter",
"backbone.wreqr",
"text",
"semantic"
], function () {
/* plugins ready */
});
define(["main"], function (App) {
App.start();
});
});
Also the baseUrl
is the same as the directory as your data-main
attributes folder (http://requirejs.org/docs/api.html#jsfiles):
RequireJS loads all code relative to a baseUrl. The baseUrl is normally set to the same directory as the script used in a data-main attribute for the top level script to load for a page. The data-main attribute is a special attribute that require.js will check to start script loading.
So I think your baseUrl
in config.js
points to scripts/scripts/
-folder which doesn't exist. It could/should be vendor/
instead (and remove the vendor part from all of the declarations) or just left empty.
Instead of wiredep
, you could try using https://github.com/yeoman/grunt-bower-requirejs which does similar things to wiredep
but specially for bower/requirejs
apps (see: https://github.com/stephenplusplus/grunt-wiredep/issues/7)
Your repo doens't include the dist-folder for jQuery
, but otherwise here's a working sample of config.js
: http://jsfiddle.net/petetnt/z6Levh6r/
As for the module-definition, it should be
require(["dependency1", "dependency2"])
and the module should return itself. Currently your main
file sets itself as a dependency
require(["main", "backbone", "marionette"], function(App, Backbone, Marionette){
As you already set the backbone
and marionette
as globals with exports
, you can again set the function attributes empty, so your main file should look like this:
require(["backbone", "marionette"], function(){
"use strict";
var App = new Backbone.Marionette.Application();
App.addInitializer(function(){
console.log("hello world!");
Backbone.history.start();
});
return App;
});
And as you already use define
to load main
, don't require
it again. Instead just call App.start()
inside the define
function.
https://jsfiddle.net/66brptd2/ (config.js)
seoyoochan
Location: 🇰🇷 Seoul, South Korea Job Title: Full Stack Engineer 💎 Tech Stack: Ruby on Rails, Javascript, Ruby, Go, Swift, Linux, Nginx, HTML, CSS, MySQL, Redis, Postgresql DevOps: Docker, Packer, Vagarant, Chef, Capistrano CI Tools: Jenkins, Travis, Codeship
Updated on July 09, 2022Comments
-
seoyoochan almost 2 years
I have struggled a few days to figure this out,, but finally I need your help today.
my repo: https://github.com/seoyoochan/bitsnut-web
what I want to achieve:
- Load and optimize r.js - Write bower tasks for RequireJS and r.js :
tasks are: minify & uglify & concatenation for RequireJS, and optimise with r.js on production
- How to exclude js script tags inindex.html
when usingwiredep
tasks and load them through RequireJS loader?
I use Yeoman 'Webapp' generator and generated the scaffold app.
I installed backbone, marionette, text, underscore, and etc via
bower install
I modifiedbower.json
by removingdependencies
and left only"requirejs": "~2.1.16"
ondependencies
. (devDependencies
is empty)because I use
[2][grunt-wiredep]
, everything is automatically loadedbower_components
intoindex.html
. I modified.bowerrc
to store dependencies atapp/scripts/vendor
.However, the problem is that I don't know how to successfully load them through ReuqireJS and not to load the vendors as script tags inside
index.html
. I have to write some tasks for RequireJS and r.js, but don't know how to achieve this goal ( I installedgrunt-contrib-requirejs
though )I want to follow the 4th method to make use of
r.js
at https://github.com/jrburke/requirejs/wiki/Patterns-for-separating-config-from-the-main-module. but the issue I encountered was that RequireJS's documentation does suggest to not usenamed module
, butanonymous module
. I would like to hear various opinions about how I should approach.I really appreciate your help in advance!
-
seoyoochan about 9 years@PeteTNT I know I don't want it to be included in the index.html, but they were included automatically by 'wiredep'. How could I solve it? And also, are both main.js and config.js correct?
-
-
seoyoochan about 9 yearsI really appreciate your detailed answer! but I have still a few problems when loading files with requireJS. if you
grunt serve
my app from the repo, the console complains a lot of stuff .. and how can I letconfig.js
refer tomain.js
? or letmain.js
refer toconfig.js
? I thought I could just sayrequire
the defined module file set bydefine() method
, am I wrong? -
Pete TNT about 9 years@seoyoochan, I pulled your updated repo and modified the
config.js
file a bit: see the end of the page for a fiddle. The repo didn't contain the /dist/-folder forjquery
though. I didn't notice that you (correctly) set the exports, so you can / should leave the define and ready-functions attributes empty. -
seoyoochan about 9 yearsThanks! It almost works now, but when I require ["main"] inside
config.js
. it returnsundefined
. look at this s18.postimg.org/6dcw0l57t/… and I updated the repo -
Pete TNT about 9 yearsSee my edit again, it fixes some issues with the Main.
-
Pete TNT about 9 yearsAaand some racing conditions. I think it still could be improved a bit but it finally works :)
-
seoyoochan about 9 yearsI don't know why my server keeps saying
'define' is not defined.
because define was not defined successfully,var App = new Backbone.Marionette.Application();
does not work either and spits out error on console. :/ (updated repo) -
Pete TNT about 9 yearsCheck your Network-tab. It might be that your servers
baseUrl
is different than the one locally! -
seoyoochan about 9 yearsLet us continue this discussion in chat.
-
seoyoochan about 9 yearsI will enter that chat.