Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module.exports client side

Tags:

I've created a node module that is essentially just some useful JS that can also be used client side. I know that require.js can load common.js components, but I don't necessarily want to make a mandate that everyone who uses my module client side also needs require or common.js or something. I also don't want to force them to remove the module.exports = ... at the bottom of the file. How do others solve this problem? Do you just create 2 versions, or 2 "compiled" versions rather? Does module.exports work everywhere?

like image 360
Parris Avatar asked Sep 24 '12 19:09

Parris


People also ask

What can you export with module exports?

By module. exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.

How do module exports work?

Module exports are the instructions that tell Node. js which bits of code (functions, objects, strings, etc.) to export from a given file so that other files are allowed to access the exported code.

What are the two types of module exports in JavaScript?

Every module can have two different types of export, named export and default export.

Can NodeJS be client side?

js modules from JavaScript running in the browser has many advantages because it allows you to use Node. js modules for client-side JavaScript applications without having to use a server with Node. js just to implement functionality that is already available as a Node. js module that is available via an npm package.


1 Answers

This is what underscore.js does:

if (typeof exports !== 'undefined') {   if (typeof module !== 'undefined' && module.exports) {     exports = module.exports = _;   }   exports._ = _; } else {   root['_'] = _; } 
like image 62
Jonathan Ong Avatar answered Oct 18 '22 11:10

Jonathan Ong