The require function in WebApps

The require function is available in both server-side and client-side context of a WebApp. Though, not all objects are available in both contexts.

Client

Below is an example of objects available client-side via the require function. Note that only client and common modules are available.

// Client context
var
  _              = require('underscore'), // [example](https://underscorejs.org/)
  $              = require('jquery'), // [example](https://jquery.com/)
  redux          = require('redux'), // [example](https://redux.js.org/)

  router         = require('router'), // [example](/docs/webapps/sdk#h-router)
  i18n           = require('i18n'), // [example](/docs/webapps/i18n)
  events         = require('events'), // [example](/docs/webapps/component#h-Appandglobalevents)
  app            = require('app'), // [example](/docs/webapps/component#h-Appandglobalevents)
  store          = require('store'), // [example](/docs/webapps/state-management#h-Store)
  requester      = require('requester'), // [example](/docs/webapps/sdk#h-requester)

  Component      = require('Component'), // [example](/docs/webapps/component)
  ListComponent  = require('ListComponent'), // [example](/docs/webapps/component#h-ListComponent)

  MyComponent    = require('/component/NameOfComponent'), // [example](/docs/webapps/component)  

  myTemplate     = require('/template/nameOfTemplate'), // [example](/docs/webapps/template)
  
  myClientModule = require('/module/client/nameOfClientModule'), // [example](/docs/webapps/module)
  myCommonModule = require('/module/common/nameOfCommonModule'); // [example](/docs/webapps/module)

Server

Below is an example of objects available server-side via the require function. Note that only server and common modules are available.

// Server context, e.g. index.js or a server module
const
  _              = require('underscore'), // [example](https://underscorejs.org/)
  
  router         = require('router'), // [example](/docs/webapps/index.js#h-Routing)
  i18n           = require('i18n'),   // [example](/docs/webapps/i18n)
  appData        = require('appData'),       // [example](/docs/webapps/configuration/appdata)
  globalAppData  = require('globalAppData'), // since 5.2 [example](/docs/webapps/configuration/appdata)
  appResource    = require('appResource'),   // since 4.5.1 [example](/docs/webapps/resource#h-AppResource)
  appInfo        = require('appInfo'),       // since 4.5.3 [example](/docs/webapps/require/appinfo)
  storage        = require('storage'),       // since 5.0 [example](/docs/data-storage)
  oauth2         = require('oauth2'),        // since 7.0 [example](/docs/webapps/require/oauth2)
  privileged     = require('privileged'),    // since 7.0 [example](/docs/webapps/require/privileged)  

  Component      = require('Component'),     // [example](/docs/webapps/component)
  ListComponent  = require('ListComponent'), // [example](/docs/webapps/component#h-ListComponent)

  MyComponent    = require('/component/NameOfComponent'), // [example](/docs/webapps/component)  

  myTemplate     = require('/template/nameOfTemplate'), // [example](/docs/webapps/template)
  
  myServerModule = require('/module/server/nameOfServerModule'), // [example](/docs/webapps/module)
  myCommonModule = require('/module/common/nameOfCommonModule'); // [example](/docs/webapps/module)

The SiteVision Public API is always accessible server-side. All interfaces, classes and enums that are anotated with the @Requireable annotation is available via the require function.

Note that Public API requireables always has a leading capital letter (requireable name matches the name of the Public API interface/class/enum).

// Server context (e.g. index.js, server module etc.)
const
  properties             = require('Properties'),
  propertyUtil           = require('PropertyUtil'),
  resourceLocatorUtil    = require('ResourceLocatorUtil'),
  portletContextUtil     = require('PortletContextUtil'),
  permissionUtil         = require('PermissionUtil'),
  publishingUtil         = require('PublishingUtil'),
  versionUtil            = require('VersionUtil'),
  metadataUtil           = require('MetadataUtil'),
  metadataDefinitionUtil = require('MetadataDefinitionUtil'),
  outputUtil             = require('OutputUtil'),  
  userIdentityUtil       = require('UserIdentityUtil'),
  mimeTypeUtil           = require('MimeTypeUtil'),
  nodeIteratorUtil       = require('NodeIteratorUtil'),
  nodeTreeUtil           = require('NodeTreeUtil'),  
  nodeComparatorUtil     = require('NodeComparatorUtil'),
  nodeFilterUtil         = require('NodeFilterUtil'),
  nodeTypeUtil           = require('NodeTypeUtil'),
  localInboundRestApi    = require('RestApi'),
  ...   

Config [@since 5.0]

Objects available server-side via the require function in config/index.js. Note that only server modules are available.

// Server context, e.g. config/index.js
const
  _             = require('underscore'),    // [example](https://underscorejs.org/)
  
  router        = require('router'),        // [example](/docs/webapps/index.js#h-Routing)
  i18n          = require('i18n'),          // [example](/docs/webapps/i18n)
  appInfo       = require('appInfo'),       // since 4.5.3 [example](/docs/webapps/require/appinfo)
  storage       = require('storage'),       // since 5.0
  appData       = require('appData'),       // since 5.2 [example](/docs/webapps/configuration/appdata)
  globalAppData = require('globalAppData'), // since 5.2 [example](/docs/webapps/configuration/appdata)
  appResource   = require('appResource'),   // since 5.2 [example](/docs/webapps/resource)  
  privileged    = require('privileged'),    // since 7.0 [example](/docs/webapps/require/privileged)
  
  myServerModule = require('/module/server/nameOfServerModule'); // [example](/docs/webapps/module)

Pre render hooks [@since 6.2]

Objects available when evalutating pre render hooks. SiteVision's Public API is requireable, as well as a number of WebApp specific requireables.

// hooks.js context, WebApp specifics
const 
  _             = require('underscore'); // [example](https://underscorejs.org/)
  
  appData       = require('appData'),       // [example](/docs/webapps/configuration/appdata),
  appInfo       = require('appInfo'),       // [example](/docs/webapps/require/appinfo),
  appResource   = require('appResource'),   // [example](/docs/webapps/resource#h-AppResource)
  globalAppData = require('globalAppData'), // [example](/docs/webapps/configuration/appdata)
  hooks         = require('hooks'),         // [example](/docs/webapps/hooks)
  i18n          = require('i18n'),          // [example](/docs/webapps/i18n)
  storage       = require('storage'),       // [example](/docs/data-storage)
  oauth2        = require('oauth2'),        // since 7.0 [example](/docs/webapps/require/oauth2)
  privileged    = require('privileged'),    // since 7.0 [example](/docs/webapps/require/privileged)  
  
  serverModule  = require('/module/server/moduleName'); // [example](/docs/webapps/module)