1
votes

I started learning JS and at the moment I am working on Require.js. Here is the deal, I have simple html page and js in it:

<script data-main="js/main.js" src= "js/lib/require.js" type="text/javascript"></script>

I do really have main.js in the root/js folder but by some reason page is looking for main.js in the root. Error:

*Failed to load resource file:///work/programs/brackets/4proj/main.js*

If I put main.js in the root (data-main="main") all will work as expected but I don't want to have a lot of *js in the root, even 2 js files. I saw in the require.js examples that it is possible but I have had no success with it.

What I've tried:

/js/main.js
./js/main.js
//js/main.js

and all the same without extension. It's still not working.

Here is project structure. Project folder - 4prog/

  • /js
  • /js/lib/ <- jquery.js,require.js
  • /css
  • index.html

p.s. I am using Ubuntu + brackets + chromium.

1
Could you describe your directory structure more clearly? Also, have you tried removing .js from data-main?Qantas 94 Heavy
Using file: URIs causes a huge pile of gotchas when dealing with dynamically loading anything. It really isn't worth doing development in an environment without a webserver.Quentin
Yes, use something like Apache. Most JS documentation doesn't mention when a server is required because it is assumed that anything doing web development will be using a server (because not using one causes all sorts of problems as mentioned previously).Quentin
For full-fledged web development running a server of some sort becomes a necessity. However, I've run dozens of mini RequireJS demos composed of multiple files, all served from file: to test ideas (and to answer SO questions): none of them required a server to run properly.Louis
@Noob.Net What I'm seeing in the question seems okay to me. If you pass a configuration to RequireJS, it would be good to see it.Louis

1 Answers

1
votes

From the docs:

http://requirejs.org/docs/api.html#jsfiles

.js does have meaning to require - it makes it behave like an absolute path.

So does prefixing with "/".

You didn't explicitly mention trying

<script data-main="js/main" src= "js/lib/require.js" type="text/javascript"></script>

Which is what works on my system (from file: !), so I can only assume that either you've got something else going on, or you didn't try that.

You didn't mention your version of require, your OS, or your browser, so that could be it, but it's more likely that trying to do an absolute path with require is messing you up without serving from a web server.