# Sharing code across workflows

Pipedream provides two ways to share code across workflows:

  • Create an action. Actions are reusable steps. When you author an action, you can add it to your workflow like you would other actions, by clicking the + button below any step. Learn how to build your first action here.
  • Create your own npm package. If you need to run the same Node.js code in multiple workflows, you can publish that code as an npm package. We'll walk you through that process below.

# Publishing your own npm package

# The short version

  1. Follow this guide (opens new window) to publish your code as an npm package. You can see the code for an example package here (opens new window), @dylburger/pd.
  2. In any workflow, you can import code provided by your package.
// import the random function from this example package
import { random } from "@dylburger/pd";
console.log(random());

# Step by step

This guide will walk you through how to create and publish an npm package. You can import the code from this package in any Pipedream workflow.

  1. Open the publishing guide (opens new window) from npm. Follow steps 1 - 7.
  2. Create an index.js file within your package's directory with the following contents:
function random() {
  return Math.random();
}

// Read https://www.sitepoint.com/understanding-module-exports-exports-node-js/
// for more information on this syntax
export default {
  random,
};

The random function is just an example - you can keep this code or replace it with any function or code you'd like to use on Pipedream.

  1. You'll need to publish a public package to use it on Pipedream. Make sure to review your code for any sensitive information (opens new window).
  2. Follow the instructions in the Publishing scoped public packages section (opens new window) of the npm guide to publish your package to npm's registry.
  3. In a Pipedream workflow, import the random function from the example, or run the other code provided by your package:
// import the random function
import { random } from "@your-username/your-package";
console.log(random());
  1. If you need to add more code to your package, add it to your index.js file, increment the version in your package.json file, and publish your package again.

Still have questions?

Please reach out if this doc didn't answer your question. We're happy to help!