admin管理员组

文章数量:1415119

How can I make the node.js require function in pure JavaScript? I have searched the web for any solutions but I have found none.

Something along the lines of this:

function require(path){
    //Code
    return data;
}

Some of the posts I have already seen:

  • How does require() in node.js work?
  • Node.js "require" function and parameters

How can I make the node.js require function in pure JavaScript? I have searched the web for any solutions but I have found none.

Something along the lines of this:

function require(path){
    //Code
    return data;
}

Some of the posts I have already seen:

  • How does require() in node.js work?
  • Node.js "require" function and parameters
Share Improve this question edited May 23, 2017 at 11:58 CommunityBot 11 silver badge asked Jan 27, 2014 at 2:12 ProgoProgo 3,4907 gold badges30 silver badges44 bronze badges 2
  • This looks interesting. Why is it necessary to implement the require function in pure JavaScript? – Anderson Green Commented Jan 27, 2014 at 2:17
  • It entirely depends on how you're able to locate & load other scripts. – SLaks Commented Jan 27, 2014 at 2:36
Add a ment  | 

3 Answers 3

Reset to default 1

CommonJS functionality is implemented in the Curl Library As pointed out in other answers RequireJS follows the AMD spec which is different than Node's CommonJS var foo = require('foo'); spec.

Curl Features at a glance:

  • Loads AMD-formatted javascript modules in parallel
  • Loads CommonJS/node modules (v1.1 when wrapped in a define())
  • Loads CommonJS/node modules (unwrapped when using the cjsm11 loader)
  • Loads non-AMD javascript files in parallel, too.
  • Loads CSS files and text files in parallel
  • Waits for dependencies (js, css, text, etc) before executing javascript
  • Waits for domReady, if desired
  • Allows for virtually limitless binations of files and dependencies
  • Tested with Safari 5+, IE6+, and recent Chrome, FF, Opera

Browserify does not implement CommonJS straight out but rather transpiles your CommonJS style code into a single bundle.js file which is included in the html.

Webmake seems to offer similar functionality to Browserify while also allowing for other file types.

In a nutshell CommonJS works like this:

  1. Maintain a map of identifiers that correspond to loaded modules.
  2. When a require('lib')is encountered, check the list to see if it has been loaded. If the library has not been loaded (it's key is not in the library), load the script via XHRRequest. If the library has been loaded, return the value that corresponds to the key.

This is why required modules are always singletons.

RequireJS uses AMD loader, whereas node.js uses CommonJS pattern loading.

Browserify allows you to wrap up all of your code in a build step so you can have require() statements in a browser.

To roll your own (which I do not suggest, as Browserify is already a battle-tested, popular library), you would have to map all of your modules on your own and load it upfront, or load them synchronously (as that's necessary for CommonJS spec), in a blocking (and undesirable) way.

code in your function should contain full module resolution logic, which may not be that light and simple. You can check source code of module module which is responsible for that in node.js -> https://github./joyent/node/blob/master/lib/module.js

There are various tools which help you port CJS modules outside of node.js (they provide require function for you): Most popular is Browserify, but you can also check Webmake (I'm the author).

本文标签: How to make the nodejs require function in pure JavaScriptStack Overflow