Is it possible to use require()
(or something similar) on client side?
Example
var myClass = require('./js/myclass.js');
It is possible to use require() on the client browser side. For example: var myClass = require('./js/myclass.
1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.
It lets you use require() in the browser by bundling up the dependencies that you have in your program. I'll show you how to use this extension for your projects.
“Require” is built-in with NodeJSWith require , you can include them in your JavaScript files and use their functions and variables. However, if you are using require to get local modules, first you need to export them using module. exports . For example, let's assume that you have a file called blogDetails.
You should look into require.js or head.js for this.
I've been using browserify for that. It also lets me integrate Node.js modules into my client-side code.
I blogged about it here: Add node.js/CommonJS style require() to client-side JavaScript with browserify
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With