How to Correctly Define and Use Utility Functions in Workflow Code Steps?

This topic was automatically generated from Slack. You can find the original thread here.

Hi, I’m struggling with defining utility functions for use in my workflow code steps…

I spoke to Pi and it said to publish a custom action with code like this:

export default defineComponent({
  async run({ steps, $ }) {
    // Define your reusable function
    const formatData = (data) => {
      // Your data formatting logic here
    }
    
    // Export the function to make it available
    return {
      formatData
    }
  }
})

Then use it in workflow code steps like this:

// Method 1: Import via props
export default defineComponent({
  props: {
    formatter: { 
      type: "component",
      path: "/components/my-formatter"
  },
  async run({ steps, $ }) {
    // Access the function through props
    const formattedData = this.formatter.formatData(data)
  }
})

Or this:

// Method 2: Direct Import
import { formatData } from "/components/my-formatter"
export default defineComponent({
  async run({ steps, $ }) {
    // Use the imported function directly  
    const formattedData = formatData(data)
  }
})

Neither method is working for me at the mo.
Method 1 fails with: **UserError** unsupported type for prop formatter: component
Method 2 fails with: **Error** ENOENT: no such file or directory, open '/components/my-formatter.js'

Can anyone explain how to do this correctly (is it possible)?

Code steps aren’t published the same way as actions/sources through the CLI.

I recommend publishing your utility as an action, then you can use it in other workflows under the My Actions category.

Here’s more details on how do that: GitHub