Git Product home page Git Product logo

Comments (15)

eboto avatar eboto commented on May 27, 2024

This is a sample project that will generate the error.

Just make sure you have coffee-script installed via npm and run ./runtest.sh from the rjsexample directory that gets unarchived.

from r.js.

eboto avatar eboto commented on May 27, 2024

Just clicked my own link. Wow...really wish I knew of a not shitty file sharing site.

from r.js.

jrburke avatar jrburke commented on May 27, 2024

I believe this issue occurs because node is using the r.js location for the node_modules directory, instead of the location of your main script. I thought I improved this in the r.js release 0.25.0, although I will need to change the mechanism for Node 0.5.3+ since they removed require.paths. I am tracking the Node 0.5.3 change in issue #11.

Can you confirm what version of r.js you are using? If not 0.25.0, trying that may work better as long as you are using Node 0.4.x.

from r.js.

jrburke avatar jrburke commented on May 27, 2024

Actually I don't think what I did will make a difference, the path lookup in node is rooted around the current module script, so it will start where r.js is.

To prove it, I suspect if you put r.js in the node_modules directory that has the coffeescript package, I expect it will work. I still plan to do more testing, but may not get around to coffeescript testing for a couple days. Feel free to post back with any info if you get to it before then.

from r.js.

eboto avatar eboto commented on May 27, 2024

Hi James. I should be able to try this out by tomorrow.

from r.js.

eboto avatar eboto commented on May 27, 2024

Sorry I took so long to get to this. I can verify that the version is 0.25.0.

After playing with it some more, it appears that javascript libraries can be imported correctly relative to the test script's path, but coffee-script ones are always imported relative to the path of r.js! To prove this, take the sample project from my original post. In that sample, the original error case was that the following command fails:

> node tools/r.js script.js

However, if in script.js you change require('./coffee-library') to require('../coffee-library') it works fine! The key point being that we didn't have to change require('./js-library') at all.

The behavior of the test case you suggested supports this conclusion. I placed r.js in ~/node_modules. It still didn't work, but if I changed the .coffee require statement in script.js to require('../rjsexample/coffee-library') it worked fine!

Not yet sure how to fix it... =)

from r.js.

jrburke avatar jrburke commented on May 27, 2024

I think I have a fix, but it means loading r.js as a module. You can look at the latest two commits in this repo, see embedded/main.js as example, but require('r.js') then pass nodeRequire in the requirejs config, passing the require function from the app's main file and using the 20110808 dist/ snapshot of r.js. I mean to polish it up more and write docs, but you can do an early test of it if you like

from r.js.

eboto avatar eboto commented on May 27, 2024

Hmm I'm trying to load that build as a module but I get the following error:

> require('./r.js')
TypeError: Cannot read property 'filename' of undefined
    at /Users/eboto/rjsexample/test/r.js:8514:40
    at Object.<anonymous> (/Users/eboto/rjsexample/test/r.js:8605:1)
    at Module._compile (module.js:402:26)
    at Object..js (module.js:408:10)
    at Module.load (module.js:334:31)
    at Function._load (module.js:293:12)
    at require (module.js:346:19)
    at [object Context]:1:1
    at Interface.<anonymous> (repl.js:171:22)
    at Interface.emit (events.js:64:17)

from r.js.

eboto avatar eboto commented on May 27, 2024

Hey...maybe this is a ridiculous suggestion. But what if just before executing the target script we just hot-swapped out require.nodeRequire.main with a version that refers to the actual script path rather than to r.js? The coffee-script bug pretty much stems from its conflating '.' with 'r.js' based on a reference it keeps to nodeRequire.main in nodeRequire.cache.

So if we just hotpatched nodeRequire.main before running the script then it would run fine I think.

from r.js.

eboto avatar eboto commented on May 27, 2024

This commit explains better what I meant. I know it fixes this particular bug, but it seems like the kind of deep-in-the-guts change that could cause all sorts of later shenanigans to occur.

What do you think?

from r.js.

jrburke avatar jrburke commented on May 27, 2024

Did you try the latest test with this snapshot version? I do not get that "filename" error. Sorry I did not put the direct link in my last comment, I was commenting from my phone and did not have the URL handy.

As for changing the requireMain.filename, the code I traced in Node (0.4.9), it does not use that filename for module lookups, but rather the "paths" that are already generated on that "main" object (which I am sure are generated from the filename, but that happens before the code in x.js runs).

It also feels like reaching too much into the internals of Node. They have changed how they do pathing, and by just using the require that was in the main script that should work later if they change the rules.

from r.js.

jrburke avatar jrburke commented on May 27, 2024

I forgot to mention: here is the test I used to test the latest snapshot. It uses a require('requirejs') but a local require('./tools/r.js') should work just as well, using the latest r.js snapshot.

from r.js.

eboto avatar eboto commented on May 27, 2024

Thank you! That test clarified the expected setup quite a bit, and it runs fine for me.

Three things:
(1) Still, if I just have r.js in a directory and attempt to require it directly from that directory (require('./r');)I get that filename problem. Is that just not how it was meant to be accessed?

(2) I'm using r.js to write fast unit tests for a lot of .coffee files whose compilation product will eventually be executed in the browser. To use this workaround would I have to change that browser code to use requirejs( rather than require(?

(3) You may consider changing the test module from canvas to something a little more lightweight, preferably sans dependencies. Installing cairo (dependency of canvas) onto macos takes about 20 min =)

from r.js.

jrburke avatar jrburke commented on May 27, 2024

(1) It sounds like the test runs for you, but if you try to run it in your program, you get the "filename" error? Is your program trying to access the "filename" property from a particular object? If you run node in debug mode, like via node-inspector, and then look at the call stack, what code is trying to read "filename"? I expect it to be in a particular module, and not something in r.js itself.

(2) For top-level require() calls in the browser, as of RequireJS 0.25.0, requirejs() is an alias to require() so you can use it in the browser. The other option is to require() any normal node modules, then do a require = require('requirejs') if you prefer to always use "require" in your code, but from then on in that script, require() will be requirejs' require() and not node's require. Hmm, and you want to save off node's require, so that you can pass it to requirejs, so maybe:

var nodeRequire = require;
var require = require('requirejs');

(3) Agreed, if you have a suggestion for a simpler module, I would rather use that than canvas. I just used canvas because I had an existing test that used it.

from r.js.

eboto avatar eboto commented on May 27, 2024

(1) Ahh I just tested out...turns out the "filename" error only appears when _require_ing requirejs from the REPL. Works fine from file. I assume this is because filename refers to the script being run which, in the case of the REPL, is correctly undefined.

(2) No, typing requirejs is fine. And accessing requirejs as a module is wonderful; something always rubbed me a bit strange about passing my application as a secondary command line argument just because it uses requirejs.

(3) How about uglify-js? It doesn't seem to have any deps...

from r.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.