How to reuse the code such as creating include file, and then reference it from within my workflows?

I know I can create a full featured reusable component and have an elaborate folder structure, put my reusable code inside some ‘common’ folder, and then import common from "../common/common-webhook.mjs";

My problem is, I don’t need a component as a standalone step, but instead, I would like to isolate my constants, at least graphql strings which tend to be rather long, at least in my case, or a few simple functions that I need to call several times and are common across most of my workflows.

I think I’m missing something super obvious,

Please help me understand,
Thank you,

Hi @outbakery,

To manage reusable code such as constants, GraphQL strings, or simple functions that are common across your workflows without creating a full-featured component, you can leverage the concept of custom Node.js modules within your Pipedream workflows. This approach allows you to isolate and import reusable code snippets. Here’s a step-by-step guide on how to achieve this:

  1. Create a Reusable Module:

    • First, create a new file for your reusable code. For example, you might create a file named constants.mjs or utils.mjs in your project directory. Note that we use the .mjs extension to denote that it’s an ES Module, which supports the import and export syntax.
  2. Add Your Reusable Code:

    • Inside your module file (e.g., constants.mjs), define your constants, GraphQL strings, or functions. For example:
      // constants.mjs
      export const GRAPHQL_QUERY = `your graphql query string here`;
      export const API_KEY = 'your-api-key';
      
      export function calculateSomething(value) {
        return value * 2; // Just an example function
      }
      
  3. Import and Use Your Module in Workflows:

    • In your workflow’s code step, you can now import the module you created. Since workflows run in a serverless environment, you’ll need to ensure that your module is accessible from your code step. One approach is to host your module file on a public GitHub repository or a similar platform, then use dynamic import() to load your module. For example:
      // Assuming your module is hosted at https://example.com/path/to/constants.mjs
      const { GRAPHQL_QUERY, calculateSomething } = await import('https://example.com/path/to/constants.mjs');
      
      console.log(GRAPHQL_QUERY); // Use your imported constant
      console.log(calculateSomething(10)); // Call your imported function
      
    • Note: Hosting your module publicly may not be suitable for sensitive information. In such cases, consider alternatives that align with your security requirements.
  4. Repeat as Needed:

    • You can import and use your module in any number of workflows, promoting code reuse and maintainability.

This approach allows you to keep your workflows organized and avoid duplicating code across them. If you have any further questions or need more assistance

I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double-check my answers and code! Learn how to build your own.

When I’m inside my project, I have a NEW button that only has 2 options. New Folder or New Workflow. I don’t have any other options.
Am I looking at the wrong place?
Thank you,

You could try using the File Store to store & retrieve your reusable code.