Why is My Node.js Script Not Exporting Anything Despite Successful Execution?

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

I have this script:

async (event) => {
  // Define your headings array here, adjusted for direct use within an inline Node.js step
  const headings = [
    "Submission Date", "Valuation Provider", "Last Update Date", // continue with your actual headings...
  ];

  // Initialize collectedInformation, assuming event.newRow holds the array of data
  let collectedInformation = [];

  // Ensure that newRow exists and is an array
  if (event.newRow && Array.isArray(event.newRow)) {
    collectedInformation = event.newRow.map((item, index) => {
      // For each item in newRow, create an object with the heading as the key (if exists) or a default key
      const key = headings[index] || `Unlabeled ${index}`;
      return { [key]: item };
    });
  } else {
    console.log("newRow is not available or is not an array.");
    // Returning an error message or empty data if newRow is not as expected
    return { error: "newRow data is missing or invalid." };
  }

  // Convert the collected information into a JSON string for easy logging or further use
  const jsonString = JSON.stringify(collectedInformation);
  console.log("Collected Information:", jsonString);

  // Returning the collected data as an object
  // This makes the JSON string available as 'collectedData' to subsequent steps in the workflow
  return { collectedData: jsonString };
}

It is executing successfully, but not exporting anything. Any idea why?

Based on the script you’ve provided, it appears you’re using the syntax from Pipedream’s v1 platform, where you could assign arbitrary properties to this within a Node.js step, and the properties would be available as step exports. However, in Pipedream’s current version (v2), you should use $.export to export data or simply return data from the run method of a component. To fix your script and ensure it exports data correctly in Pipedream v2, you should convert it into a Pipedream component format and use the $.export method or return an object directly from the run method. Here’s how you can adjust your script to work correctly: import { defineComponent } from "pipedream"; export default defineComponent({ async run({ steps, $ }) { // Define your headings array here, adjusted for direct use within an inline Node.js step const headings = [ "Submission Date", "Valuation Provider", "Last Update Date", // continue with your actual headings... ]; // Initialize collectedInformation, assuming steps.trigger.event.newRow holds the array of data let collectedInformation = []; // Ensure that newRow exists and is an array if (steps.trigger.event.newRow && Array.isArray(steps.trigger.event.newRow)) { collectedInformation = steps.trigger.event.newRow.map((item, index) => { // For each item in newRow, create an object with the heading as the key (if exists) or a default key const key = headings[index] || `Unlabeled ${index}`; return { [key]: item }; }); } else { console.log("newRow is not available or is not an array."); // Returning an error message or empty data if newRow is not as expected return { error: "newRow data is missing or invalid." }; } // Convert the collected information into a JSON string for easy logging or further use const jsonString = JSON.stringify(collectedInformation); console.log("Collected Information:", jsonString); // Returning the collected data as an object // This makes the JSON string available as 'collectedData' to subsequent steps in the workflow return { collectedData: jsonString }; }, }); This script is now in the format of a Pipedream v2 component, which should correctly export the `col

I’m getting this error:

Code Error
Identifier 'defineComponent' has already been declared. (35:10)

DETAILS
SyntaxError: Identifier 'defineComponent' has already been declared. (35:10)
    at toParseError (/var/task/node_modules/@babel/parser/src/parse-error.ts:81:19)
    at raise (/var/task/node_modules/@babel/parser/src/tokenizer/index.ts:1487:19)
    at checkRedeclarationInScope (/var/task/node_modules/@babel/parser/src/util/scope.ts:143:19)
    at declareName (/var/task/node_modules/@babel/parser/src/util/scope.ts:104:12)
    at declareName (/var/task/node_modules/@babel/parser/src/plugins/flow/scope.ts:30:11)
    at registerFunctionStatementId (/var/task/node_modules/@babel/parser/src/parser/statement.ts:1683:16)
    at parseFunction (/var/task/node_modules/@babel/parser/src/parser/statement.ts:1646:12)
    at parseFunctionStatement (/var/task/node_modules/@babel/parser/src/parser/statement.ts:1032:17)
    at parseStatementContent (/var/task/node_modules/@babel/parser/src/parser/statement.ts:460:21)
    at parseStatementLike (/var/task/node_modules/@babel/parser/src/parser/statement.ts:417:17)

It looks like you’re encountering a SyntaxError because defineComponent has been declared more than once in your code. This could happen if you accidentally imported or declared defineComponent multiple times within the same scope or file. To resolve this issue, ensure that defineComponent is only declared once. In a Pipedream component, defineComponent is a global function provided by the Pipedream environment for defining components, so you do not need to import or declare it yourself. Here’s a basic structure of how your component should look: export default defineComponent({ props: { // Define your props here }, async run({ steps, $ }) { // Your code goes here }, }); Make sure that your code does not include multiple export default defineComponent({...}) declarations or attempts to redeclare defineComponent anywhere. If you have multiple components, they should be in separate files or properly managed to avoid naming conflicts. If you continue to experience issues, please review your code for any accidental redeclarations and adjust accordingly. If the problem persists, you might want to visit Pipedream Support for further assistance.

Here’s the script:

async ({ steps, $ }) => {
  const headings = [
    "Submission Date", "Valuation Provider", "Last Update Date", // and so on for your actual headings...
  ];

  let collectedInformation = [];

  // Check if newRow exists and is an array. Adjust the reference to match your actual data path
  if (Array.isArray(steps.trigger.event.newRow)) {
    collectedInformation = steps.trigger.event.newRow.map((item, index) => {
      const key = headings[index] || `Unlabeled ${index}`;
      return { [key]: item };
    });
  } else {
    console.log("newRow is not available or is not an array.");
    return { error: "newRow data is missing or invalid." };
  }

  const jsonString = JSON.stringify(collectedInformation);
  console.log("Collected Information:", jsonString);

  // Returning the JSON string as 'collectedData'
  return { collectedData: jsonString };
}

Again, it’s executing successfully but not exporting anything. Lil’ hep?

To export data from a Node.js code step in Pipedream, you can use the $.export method to create named exports, which then become accessible to downstream steps in your workflow. In your script, you’re using the return statement to return data, which is also a valid way to pass data to downstream steps. However, if you’re not seeing the exported data, it might be due to how you’re trying to access it in subsequent steps or there might be an issue with the workflow setup. Here’s a revised version of your script using $.export to explicitly name the export. This method ensures that the exported data is easily accessible in downstream steps by the given name: export default defineComponent({ async run({ steps, $ }) { const headings = [ "Submission Date", "Valuation Provider", "Last Update Date", // and so on for your actual headings... ]; let collectedInformation = []; // Check if newRow exists and is an array. Adjust the reference to match your actual data path if (Array.isArray(steps.trigger.event.newRow)) { collectedInformation = steps.trigger.event.newRow.map((item, index) => { const key = headings[index] || `Unlabeled ${index}`; return { [key]: item }; }); } else { console.log("newRow is not available or is not an array."); $.export("error", "newRow data is missing or invalid."); return; } const jsonString = JSON.stringify(collectedInformation); console.log("Collected Information:", jsonString); // Exporting the JSON string as 'collectedData' $.export("collectedData", jsonString); }, }); In this revised script, I’ve used $.export("collectedData", jsonString); to export the JSON string under the name collectedData. This makes it accessible in downstream steps as steps.[STEP_NAME].collectedData, where [STEP_NAME] is the name you’ve given to this step in your workflow. If you continue to experience issues with exporting data, ensure that you’re referencing the exported data correctly in downstream steps and that your workflow is configured properly. If the problem persists, consider visiting Pipedream Support for further assistance.