# 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
- Follow this guide to publish your code as an npm package. You can see the code for an example package here,
@dylburger/pd
. - 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.
- Open the publishing guide from npm. Follow steps 1 - 7.
- 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.
- You'll need to publish a public package to use it on Pipedream. Make sure to review your code for any sensitive information.
- Follow the instructions in the Publishing scoped public packages section of the npm guide to publish your package to npm's registry.
- In a Pipedream workflow,
import
therandom
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());
- If you need to add more code to your package, add it to your
index.js
file, increment theversion
in yourpackage.json
file, and publish your package again.