Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript require() on client side

Is it possible to use require() (or something similar) on client side?

Example

var myClass = require('./js/myclass.js'); 
like image 415
Debra Maddux Avatar asked Mar 02 '11 13:03

Debra Maddux


People also ask

Can you use require in client-side JavaScript?

It is possible to use require() on the client browser side. For example: var myClass = require('./js/myclass.

What does require () do in JavaScript?

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.

Can you use require in the browser?

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.

Can we use require in JavaScript?

“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.


2 Answers

You should look into require.js or head.js for this.

like image 132
Andrew Hare Avatar answered Sep 24 '22 17:09

Andrew Hare


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

like image 43
dkastner Avatar answered Sep 24 '22 17:09

dkastner