New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shims not being shimmed within node.js #703
Comments
As sorry for the confusion -- shim config is only meant for browser use. I cannot replicate it in node's environment, since each script is executed in its own sandbox, so things like I will keep this ticket open to do a doc change to indicate that shim config is only for browser use. |
It would be helpful if the library would still resolve the shims and issue an error (throw an exception, maybe?) when the user is trying to load a shim from node. It is better for the user to have it fail properly rather than just failing silently like it does now :) That being said, if you were to find a magical way of making the shims work in node as well, I would be grateful :) |
Good idea, I'll see about throwing in the node case to give an earlier clear error. I do not see a way to get it to work though. |
@jrburke I had shim config working fine in my node based unit tests, but now it breaks because you are forcibly throwing an error. Any chance you would consider reverting this change and making it documentation only… I see the predicament you are in (Should it fail silently, throw an error, or do something else). Anyway, I'll just set my npm version back to pre 2.1.6 for the time being. |
@jrburke Let me clarify, my unit tests were running fine (vs. "shim config working fine") – I guess I worked around the shim issue, but now my unit tests are being killed by the error alerting me to the lack of shim support. |
I am definitely open to just making this maybe just a trace log statement and a doc blurb, but I would like to understand more how the unit tests run in such a way that they pass. As in, is it just a fluke or is it a setup others may use and therefore may hit this and limit their productivity. Tentatively putting this in a 2.1.7 bucket, but would like to understand it more, since I obviously did not understand how shim might be used in this context well enough to make this change in the first place. |
For what it's worth, I think it is strange API design to make it do nothing when requiring modules that match configured shims. That's hardly an appropriate way to implement When introducing better error reporting it shouldn't be entirely unexpected that somebody relies on former undocumented behaviour that was just there by accident. Common ways to handle this include introducing a configuration option to disable the new error reporting, or make it into an ugly warning. This way, projects that rely on the former, undocumented behaviour can get some leeway to adjust to the more sane way of handling this error situation. To be clear: I think it is absolutely imperative for any API that it communicates clearly when it can not fulfill the contract for a given call. |
@jrburke Sorry for the delay… it very well may be it worked for me because of how I was using Require for my tests. I'll try to make a reduced test case for you so you can see what I did. |
@jrburke I worked on putting together a proof of concept, but while it worked for Underscore and Backbone, it failed with a custom file I tried to shim. What I eventually found out is that files that just declare Please see my example project here: https://github.com/a2labs/require-shim-poc to see a working example. I also tried hacking the req.makeNodeWrapper = function (contents, shim) {
return '(function (require, requirejs, define) { ' +
contents +
( shim && shim.exports ? '\n;if( typeof this.' + shim.exports + ' === "undefined" ) this.' + shim.exports + " = " + shim.exports + ";" : '' ) +
'\n}(requirejsVars.require, requirejsVars.requirejs, requirejsVars.define));';
}; (I pass in the shim in the I see two options for shims in Node:
I hope this helps. Let me know if you need me to do more digging for you. |
Same problem here. I update to 2.1.6, and it broke my tests. I use the same requirejs config for the browser code, and for my tests, which run in node. And because shim config is not supported anymore I had to revert to 2.1.5. |
@dcneiner thanks for diving into it. I am not comfortable just creating variables. I expect the So what I am considering instead is just logging when shim config is in use in node and then log a message along the lines of "you may encounter errors, see [url] for more information" where [url] points to more info on the problem. Then removing the throw. |
@jrburke I think that makes a lot of sense – it provides the message for those truly making a mistake, while not breaking it for those of us intentionally utilizing the feature. |
I have a similar use-case as @dcneiner and @giggio, using the same browser-side configuration for Node-side unit tests (and nothing else). Is unit-testing considered a reasonable scenario for allowing shims in Node? Are there any other scenarios? Would it make sense to allow some "test mode" that supports Node-side shims. Even logging a warning might be overkill for a project with many unit tests. Alternatively, an example of how to test browser-side shimful configurations in Node/headless-mode — without having to maintain a modified configuration — would be really helpful. |
@gingi the best solutions are to use modular code instead of shimmed code, or use a real browser for tests, like running them in phantomjs, or some web browser driver. The node env is not the browser env, so there is only so much that can be done to make it look like one. For me, it would be a mistake to go too far with it, as node is really just focused on server developer needs, not browser developer needs. When I put in the log message, I will look at a way to disable it maybe via a config setting, but nothing promised until I have some time to really get into the code. |
Fixed in requirejs/r.js@55507ab Now it just warns for each module that is loaded that has a shim config, and the warning can be suppressed via: requirejs.config({
suppress: {
nodeShim: true
}
}); |
@jrburke |
Fixed the URL, it had a trailing colon. 2.1.7 is released now, and it has this fix. |
2.1.7 works like a charm, thanks for all your work on this @jrburke – really appreciate it! |
Great! Just updated my code and it works! Thanks again! |
The following works in node, where requirejs is an instance of the browser-side require library, and require is standard node. Modifying it to be dynamic based on the shim config shouldn't be all that hard. The raw text is passed to Function(), to get around 'use strict' in the outer context. box2dweb has 'var Box2D' at the top, and doesn't use the window or this method. function evalLoader(name, file) { requirejs.define('box2dweb', evalLoader('Box2D', 'bower_components/box2dweb/Box2dWeb-2.1.a.3.js')); |
Consider the following files for a demonstration of code that works in the browser but fails in node:
shim-module.js:
index.js (node.js entry point):
index.html:
The problem seems to be that a dependency marked as a shim in node is never the less loaded as if it were an AMD module. This is verifiable by rewriting the module as an AMD and observing that it then works.
How to reproduce:
npm install requirejs
.Expected: The node module should also output "Executing shim-module.js" and "Loaded OK".
Observed: Instead, it outputs "Executing shim-module.js" and "undefined", indicating that nothing has been exported from the shim module, even though it was executed.
The text was updated successfully, but these errors were encountered: