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?

2

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

6 Comments

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.
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).
... 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.
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.
That's awesome, on the template edit and the meteor info. I was totally unaware. Thanks
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.