Reputation: 2267
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?
Upvotes: 1
Views: 132
Reputation: 7687
This is the module template I use for module to work with both node and require.js:
https://gist.github.com/epoberezkin/5020250
Upvotes: 1
Reputation: 1541
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.
Upvotes: 2
Reputation: 163282
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.
Upvotes: 1