What is the solution to the 'require is not defined in es module scope' error?

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

hello everyone, i am getting an error:
referenceerror: require is not defined in es module scope, you can use import instead
can anyone help?

Hello , I think this is because you’re using require statement together with the import statement in the Pipedream Node.js action.

To solve this, you can try to change the require statement to use import . You can refer to this document for more detail

Actually I am not using require with import . The problem is that, when I add

export default defineComponent({
  async run({ steps, $ }) {
    const variableName = steps.trigger.event
  },
})

part to the code, the code raises this error:
Referenceerror: require is not defined in es module scope, you can use import instead
Without the definecomponent module the code works fine, but when I try to get value from the trigger event, the code raises error

Could you share the screenshot of your code step?

I think it’s because you have defined the require statement in line 8. Could you move line 8 to line 2?

I have moved it, still the same error occurs

Then maybe the @ffmpeg package is the problem. Could you try to see if the package support es module?

Then try to convert the require statement to import statement

then different error occurs

the problem is that I cannot get a value form the previous trigger step

Otherwise, my code works

for example this works fine

You’ll need the defineComponent to reference to the variables in the previous step. Would you mind checking again if you ffmpeg package support es module? You can refer to their npm page for more detail.

Also I see that the ffmpeg package uses event emitter for notifying when it is done processing. So you might need to create a Promise in order to avoid the action to stop early

For anyone needing to extract youtube audio in future, I have this working, ffmpeg too:

import ffmpeg from '@ffmpeg-installer/ffmpeg';
import YoutubeMp3Downloader from 'youtube-mp3-downloader';

// To use previous step data, pass the `steps` object to the run() function
export default defineComponent({
  async run({ steps, $ }) {
    //Configure YoutubeMp3Downloader with your settings
    var YD = new YoutubeMp3Downloader({
      "ffmpegPath": ffmpeg.path,        // FFmpeg binary location
      "outputPath": "/tmp/",    // Output file location (default: the home directory)
      "progressTimeout": "5000",
      "outputOptions": ["-ac", "1", "-ab", "64k"]
    });

    // Initiate download of the video specified in the trigger.
    const url = steps.trigger.body.url;
    const videoId = url.match(/(?:v=|\/v\/|\/embed\/|\/g\/|\/shorts\/)([^&\?\/]+)/)[1];
    YD.download(videoId);

    // Log progress
    YD.on("progress", (progress) => {
      console.log(`Progress: ${JSON.stringify(progress)}`);
    });

    // Wait for download & extraction to finish before exiting.
    let finishedPromise = new Promise((resolve, reject) => {
      YD.on("finished", (err, data) => {
        if (err) reject(err);
        else resolve(data);
      });
      YD.on("error", (error) => {
        reject(error);
      });
    });
    try {
      const data = await finishedPromise;
      console.log(`Finished: ${JSON.stringify(data)}`);
      return data.file;
    } catch (err) {
      console.error(`Error: ${err}`);
      throw new Error(`Error: ${err}`);
    }
  },
})