1

How do I write a single .js file and call (require) it from both node and the browser without making any changes?

An example of where this is useful is Model "classes".

What if the Model has dependencies, such as jquery, which are available on both node and the web?

bluekeys
  • 2,217
  • 1
  • 22
  • 30

3 Answers3

2

You could use have a look to requirejs which allows async module load in the browser and it's also possible to use in node.

jloriente
  • 1,521
  • 2
  • 10
  • 8
1

Both the browser and Node can use JavaScript just fine. There is nothing you have to do to run your code in both.

Note however if you use packages available only in Node, or browser objects (such as navigator), then you will have trouble. You should separate your code into modules that can be easily loaded in both contexts.

In addition, be sure that your code doesn't use newer JavaScript functions, or you will have compatibility problems with older browsers.

Brad
  • 159,648
  • 54
  • 349
  • 530
1

This is the module template I use for module to work with both node and require.js:

https://gist.github.com/epoberezkin/5020250

esp
  • 7,314
  • 6
  • 49
  • 79
  • That'll do me for an acceptable answer. I've been using the meteor framework recently, collections are shared on client and server. Pretty cool stuff. Check it out. – bluekeys Mar 17 '13 at 00:59
  • I've considered meteor very seriously for my current project and even did the first mockup in it. I decided not to use it for several reasons: 1) no page load on the request - bad for search engines and I hate empty pages on page load - also when the client is not very fast or something fails in JavaScript all you get is empty page. 2) our target users use IE a lot, so we needed a different loading solution, gracefully falling back on hashbangs when history API is not working, and still not breaking hash anchors the way Backbone does (we use hash anchors for popups). – esp Mar 17 '13 at 09:52
  • ... Also I disagree with fibers decision - it kills node's edge. What is really needed is a more lightweight framework doing what meteor does (unified routing in client and server, application caching, assembling client modules and templates, shared modules, shared models, etc. - they did a lot, but in a wrong way in my opinion. Almost all these things we had to do ourselves or just skip), but in a more nodish way - asynchronous, event driven, and not locking you into immature modules library when there are 20k+ modules in npm, some of them top-notch. – esp Mar 17 '13 at 09:59
  • Btw, about this template - when you start using require.js optimizer, define function from this template needs name parameter, and the name you use in define call should include require path - just edited the gist. Now it keeps working both when all JS files are bundled in one and when they are separate. – esp Mar 17 '13 at 10:07
  • That's awesome, on the template edit and the meteor info. I was totally unaware. Thanks – bluekeys Mar 17 '13 at 14:32
  • Meteor is very cool, I'm very envious I can't use it :) It's just not for everything yet... They seem to be aware of their shortcomings. By the way, if at some point you figure out some elegant way to attach data to the route object (see http://stackoverflow.com/questions/14696389/attaching-data-to-req-route-in-express), I'd like to know. The alternative is to scan routes after all routes are defined at the application startup, which is not elegant too (although I do it anyway to reuse the same regexps in the browser but I already have my data attached in the described way when I scan them). – esp Mar 17 '13 at 17:26