Navigation

Upload External Dependencies

Overview

You can upload JavaScript modules from the npm registry into your Stitch application and use the uploaded external dependencies in your Functions. This allows your application to depend upon external libraries and reuse code.

To use external dependencies, you must first upload an archive of an npm node_modules folder. Stitch automatically transpiles uploaded modules to ES5 and supports most built-in modules. You can import an uploaded module in any Stitch function.

You can see a list of uploaded packages on the Functions screen of the Stitch UI under the Dependencies tab.

External Dependency Size Constraints

Uploaded node_modules zip files are subject to a 10MB size cap.

Built-In Module Support

Supported Unsupported
  • assert
  • buffer
  • crypto
  • events
  • fs
  • http
  • https
  • net
  • stream
  • timers
  • tls
  • url
  • util
  • zlib
  • child_process
  • cluster
  • dgram
  • dns
  • domain
  • os
  • path
  • punycode
  • querystring
  • readline
  • string_decoder
  • tty
  • v8
  • vm

Procedure

1

Locally Install External Dependencies

To upload external dependencies, you first need a local node_modules folder containing at least one Node.js package. You can use the following code snippet to locally install a dependency you would like to upload:

npm install <package name>

If the node_modules folder does not already exist, this command automatically creates it.

Alternative Methods of Installation

You can also configure a package.json and run the npm install command to install all packages (and their dependencies) listed in your package.json.

To learn more about npm and node_modules, consult the npm documentation .

2

Create a Dependency Archive

Now that you’ve downloaded all of your npm modules, you need to package them up in an archive so you can upload them to Stitch. Create an archive containing the node_modules folder:

tar -czf node_modules.tar.gz node_modules/

Supported Archive Formats

Stitch supports .tar, .tar.gz, and .zip archive formats.

3

Upload the Dependency Archive

Once you’ve created an archive containing your dependencies, all that’s left to do is upload them to Stitch. You can upload your dependency archive using the Stitch UI or Stitch CLI:

  1. Select Functions from the left-side navigation.
  2. Select the Dependencies tab.
  3. Click the Upload button.
  4. In the file picker, select the node_modules.tar.gz archive you just created and click Open. Stitch automatically uploads the archive file, which may take several minutes depending on the speed of your internet connection and the size of your dependency archive.
  5. Whether the operation succeeded or failed, Stitch displays a banner indicating the success or failure of the operation. If successful, the Dependencies tab displays a list of the dependencies that you included in your dependency archive. If drafts are enabled, you will also need to click Review & Deploy to apply these changes. If drafts are disabled, the change will take effect within 5 to 60 seconds depending on the size of your dependency archive.
  1. Add the node_modules archive to your /functions directory:

    mv node_modules.tar.gz ./myapp/functions
    
  2. Import your application with the --include-dependencies option:

    stitch-cli import --include-dependencies
    

Summary

You have installed and archived your external dependencies locally, uploaded them to Stitch, and deployed your application. Now you can import external dependencies in a Stitch Function.