Navigation

Import External Dependencies

Overview

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

Pre-requisites for Import

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

Node.js Built-In Module Support

Stitch currently supports a subset of Node.js built-in modules. For details on which modules are supported, see function constraints.

Usage

You can import dependencies into any Stitch function using the require keyword inside of the exports function. You cannot use ES Module import syntax. You should import dependencies in the style of a node_modules module import, since Stitch does not support file or local 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 Stitch does not support this pattern. You must place Stitch 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 Stitch 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);
};