<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/Global-Object.require.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>Global-Object.require</api:function-name><api:suggest>global-object.require</api:suggest><api:suggest>global</api:suggest><api:suggest>object</api:suggest><api:suggest>require</api:suggest><api:function mode="javascript" name="require" type="builtin" lib="Global-Object" category="JavaScript Global Object" class="javascript" hidden="false" bucket="MarkLogic Built-In Functions" prefix="Global-Object" namespace="" fullname="Global-Object.require"><api:summary>
  Imports a module at the specified location, and returns a JavaScript object.
</api:summary><api:params><api:param name="location" type="String" optional="false"><api:param-description>
    Location of the module to import.
  </api:param-description><api:param-name>location</api:param-name><api:param-type>String</api:param-type></api:param></api:params><api:return>Object</api:return><api:usage>
  <p xmlns="http://www.w3.org/1999/xhtml">The location of the module is resolved with the same rules that MarkLogic
  resolves imported modules in XQuery. </p>
  <p xmlns="http://www.w3.org/1999/xhtml">If the document with the user-specified module name does not end with a
  file extension, require() will first look for the module with the user-
  specified name appended with the configured extensions for JavaScript module,
  and then appended with the configured extensions for XQuery module.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">If the user specified module's file extension does not match any of the
  module path extension found in the MIME type configuration,
  JS-ILLEGALMODULEPATH is thrown.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">If the user does not have execute permission on an existing module, the
  above logic to resolve a module name applies as if the module is not found
  and XDMP-MODNOTFOUND is thrown if the module cannot be located.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">Once a module is located, its query language is determined to be XQuery
  if its name extension matches with any for the XQuery module; otherwise, it
  is imported as a JavaScript module.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">If the imported module is in XQuery, the public functions and variables
  defined in it are available through the returned JavaScript object as
  properties, with either the original function name as the property name, or
  with "-" in the function names removed and the following letter capitalized.
  </p>
  <p xmlns="http://www.w3.org/1999/xhtml">In the case where a namespace is shared between built-in functions and
  XQuery modules (e.g., http://marklogic.com/xdmp/spell), the returned
  JavaScript object can be used to access both the built-ins and the XQuery
  functions.  The lookup sequence is function name, followed by variable name
  then built-in name.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">If the imported module is in JavaScript, the exported API of the module
  defined through <code>exports</code> or <code>module.exports</code> object 
  is returned. </p>
  <p xmlns="http://www.w3.org/1999/xhtml">You can import JavaScript libraries with either the 
  <code>.js</code> or <code>.sjs</code> extension (with corresponding 
  mimetypes <code>application/javascript</code> and 
  <code>application/vnd.marklogic-javascript</code>). </p>
  <p xmlns="http://www.w3.org/1999/xhtml">If there is a dependency cycle, the foreign module may not have finished
  executing at the time it is required by one of its transitive dependencies;
  in this case, the object returned by "require" must contain at least the
  exports that the foreign module has prepared before the call to require that
  led to the current module's execution.</p>
</api:usage><api:example><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
  lib.sjs:
  var PI = Math.PI;
  exports.area = function area(r) {
    return PI * r * r;
  };
  Test.js
  var circle = require("lib");
  circle.area(2);
</pre></api:example><api:example><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
  var admin = require("/MarkLogic/admin");
  var conf = admin.getConfiguration();
  admin.forestGetId(conf, "Documents");
</pre></api:example></api:function></api:function-page>