Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I like the Node.JS style of JavaScript, where I can write all of my functionalit

ID: 647843 • Letter: I

Question

I like the Node.JS style of JavaScript, where I can write all of my functionalities into smaller files and then require those neatly from within my code. I'm even thinking about trying to write a framework to mimic that behavior in client-side JS.

My goal would be to implement the module loading system as accurately as possible - See Module docs.

For require(), I can use things detailed in answers to this question, most notably JQuery's $.getScript(). It seems to me that other aspects of the module loading system should be possible as well.

So I'm asking more experienced programmers here first, before I waist my time: Is there something that I'm missing that's going to cause such an attempt to fail miserably, or can this be successfully done?

Explanation / Answer

No. Node.js require() is based on the Common.js module system which uses synchronous loading.

Unless you do some server-side preprocessing (bundling) to serve all scripts at once, this is impossible in the browser, where resources are should be loaded asynchronously. You might want to have a look at browserify.

However, there is the Asynchronous Module Definition proposal which has an async require function that takes a callback. This is widely implemented in various libraries, you probably don't have come up with your own. RequireJs is a well known one, you will find others when searching for "AMD loader".

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote