Navigation

Import External Dependencies

Overview

You can import npm modules into a Realm Function and use the imported external dependencies in your code. This allows your Realm app to depend upon external libraries for code reuse.

Pre-requisites for Import

You must upload dependencies to Realm before you can import them.

Node.js Built-In Module Support

Realm currently supports a subset of Node.js built-in modules. See function constraints for details on the support of various modules.

Usage

You can import dependencies into any Realm Function using the require keyword inside of the exports function. You cannot use ES Module import syntax. Since Realm does not support file or local module import, you should import dependencies in the style of a node_modules module import. To learn more about require syntax, consult the Node.js documentation for the require keyword.

Where Do I Import Modules?

Node.js projects commonly place require statements in the global scope of each file, but Realm does not support this pattern. You must place Realm Function require statements within a function scope.

Import a Full Module

Below, you’ll find a simple example of a function that uses the require keyword inside a function to import the ramda module to call ramda’s map method:

exports = () => {
   const R = require("ramda");
   return R.map(x => x*2, [1,2,3]);
}

Import a Module Subfolder

The following example demonstrates how to use require to import a single submodule of an external dependency into a Realm Function:

exports = function(arg){
   const cloneDeep = require("lodash/cloneDeep");

   var original = { name: "Deep" };
   var copy = cloneDeep(original);
   copy.name = "John";

   console.log(`original: ${original.name}`);
   console.log(`copy: ${copy.name}`);
   return (original != copy);
};