A compatible layer for node projects to support require feature on both browser and node. Also, provide complex file loading features for browser. And it also support to load JSON files.
npm install register-js
|14||downloads in the last week|
|66||downloads in the last month|
|Version||1.2.2 last updated 8 days ago|
|Keywords||browser, compatible, node, CommonJS, register, require|
var register_js = require("regsiter-js") //Call only once for your entire project. //Then you can use it anywhere. register(exportObject, "TestNamespace.TestObject", module);
Or if you're not 100% sure register.js has loaded correctly, the following statement will do 100% correctly under both browser and Node.js:
var module = require("register-js")(exportObject, "TestNamespace.TestObject", module);
On node.js it will equal to
module.exports = exportObject;
On browser script, it will equal to
window.TestNamespace.TestObject = exportObject;
Also, register.js will provide you a require function for browser, which is almost as the same as node.
var X = require("TestObject", "TestNamespace.TestObject");
And this tool will try to find a resource named as TestNamespace/TestObject.js.
The second parameter will be regarded as the better browser option. However, you can write no 2nd parameter as well:
var X = require("TestObject");
You can set a configuration object as registerConfig:
registerConfig.root = "http://example.com/js/"
Then the searching file will be
IE 6.0 + Firefox 1.0 + Chrome 1.0 + Safari 5.0 + Opera 9.0 +
WARNING: Since there has been already a npm package named register, so we used "register-js" instead. For browser, global scope register is also available.
Since 1.1.0, you can use requireJSON for both node and browser.
This method will look for test/jsonPackage.json. Also, you can use
requireJSON.fileType = "js";
To make it work with js files.