js require vs. import

  • require
    • is not part of your standard JavaScript,
    • used to load modules in nodejs
    • a wrapper for module.require
      • module._load => load module to cache
        • // Check the cache for the requested file.
          // 1. If a module already exists in the cache: return its exports object.
          // 2. If the module is native: call `NativeModule.require()` with the filename and return the result.
          // 3. Otherwise, create a new module for the file and save it to the cache.
          // Then have it load the file contents before returning its exports
          // object.
      • module._compile =>
      • http://fredkschott.com/post/2014/06/require-and-the-module-system/
  • module
    • files and modules are in one-to-one correspondence (each file is treated as a separate module)
    • export
      • To add functions and objects to the root of your module, you can add them to the special exports object
      • exports.area = (r) => PI * r * r;
        exports.circumference = (r) => 2 * PI * r;
      • If you want the root of your module’s export to be a function (such as a constructor) or if you want to export a complete object in one assignment instead of building it one property at a time, assign it to module.exports instead of exports.
    • Variables local to the module will be private, because the module is wrapped in a function by Node.js
This entry was posted in Nodejs, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s