norb v1.0.2
Norb
Node OR Browser
detect if the script is running on node or in a browser
Getting Started
Installing
This library is easy to install by cloning the repo or installing it through npm too:
Local installation
npm install norbGlobal installation
npm install -g norbUsage
- Require Node
const norb = require('norb')(module);
//{
// isNode:boolean (true),
// isBrowser:boolean (false),
// export:[Function],
// import:[Function],
// require:[Function]
// }- Require Browser
<script src="norb.js"></script>- isNode Boolean:
trueif running in node - isBrowser Boolean:
trueif running in browser export
export(Object, name?, mode?)This method allow to export an object in the correct way, independant if the code is in a Node or a Browser environment. (?) In the Browser context you have to assign aname?that will point towindow[name]object; themode?help to decide in which way the object will be overwritten can be 0: ( conserve ) that preserve the originalwindow[name]if present, 1: ( overwrite ) that overwrite anyway, 2: ( merge ) that merge both object.NB Browser doesn't support system API and Node doesn't support some of the Broswer API
import
import(path)Enclose and import a Browser module, like it would be a Node module.NB Browser doesn't support system API and Node doesn't support some of the Broswer API
require
require(path, [name?, mode?,] callback)Require a Node module, like it would be a Browser module. In Browser the method is async so you must define a callback for take the module result; withnameparameter you can autoassign the module to thewindow[name]. (?) In the Browser context you have to assign aname?that will point towindow[name]object; themode?help to decide in which way the object will be overwritten can be 0: ( conserve ) that preserve the originalwindow[name]if present, 1: ( overwrite ) that overwrite anyway, 2: ( merge ) that merge both object.NB Browser doesn't support system API and Node doesn't support some of the Broswer API
Example
File mymodule.js
//Module to be exported
const norb = require('norb')(module);
var export = {};
export.test = function(){console.log('sample function for testing')};
norb.export(export,'myModule');File index.html (Browser)
...
<script src="./norb.js"></script>
<script>
norb.require('./mymodule.js',(module)=>module.test()); // "sample function for testing"
</script>
...File node.js (Node)
const norb = require('norb')(module);
const myModule = norb.require('./mymodule');
myModule.test(); // "sample function for testing"Contacts
If you like the project feel free to contact me on my .
Something gone wrong? Feel free to rise an issue!
Did you like this project and it was usefull? Help me improve my work: