Help, The command "Merge Layers" is not currently available

Hi guys! im creating scripts to automate my workflow.

Im trying convert white pixels to transparent in the selected black-white only pixels layer.
But it’s always throwing an error

script.psjs: The command “Merge Layers” is not currently available.

Here is the scripts, the step 1-4 is works flawlessly except the step 5.
my current assumption is because the photoshop ui haven’t update the layers list.

please help me how to fix this.

  async function makeWhiteTransparent() {
    // const doc = app.activeDocument;
    // const layer = doc.activeLayers[0];
  
    await bp(
      [
        // Step 1: Rasterize layer
        {
          _obj: "rasterizeLayer",
          _target: [{ _ref: "layer", _enum: "ordinal", _value: "targetEnum" }],
          _options: { dialogOptions: "dontDisplay" },
        },
        // Step 2: Set selection from white
        {
          _obj: "set",
          _target: [{ _ref: "channel", _property: "selection" }],
          to: { _ref: "channel", _enum: "channel", _index: 0 },
          _options: { dialogOptions: "dontDisplay" },
        },
  
        // Step 3: Cut white pixels
        {
          _obj: "cut",
          _options: {
            dialogOptions: "dontDisplay",
          },
        },
        // Step 4: Duplicate the results
        {
          _obj: "duplicate",
          _target: [
            {
              _ref: "layer",
              _enum: "ordinal",
              _value: "targetEnum",
            },
          ],
          _options: {
            dialogOptions: "dontDisplay",
          },
        },
        // Step 5: Merge result original and duplicated
        {
          _obj: "mergeLayersNew",
          _options: {
            dialogOptions: "dontDisplay",
          },
        },
      ],
      {
        immediateRedraw: true, // default
        synchronousExecution: true, // default
      }
    );
  }

also the step 5 scripts is works on Alchemist Dispatch tool

First - please format your code properly
Second - merging layers changes the state and you need to do it in a modal state with executeAsModal

thanks it’s fixed now.

this is my error code

  await core.executeAsModal(
    async (e) => {
      await placeFile(file);
      await transformMoveLayer(boundsTop, boundsLeft);
      await makeWhiteTransparent();
    },
    { commandName: "Place Linked, Move and Apply Transparency" }
  );

fixed by changing the code to this

  await core.executeAsModal(
    async (e) => {
      await placeFile(file);
      await transformMoveLayer(boundsTop, boundsLeft);
    },
    { commandName: "Place Linked and Move" }
  );

  await makeWhiteTransparent();