Form api AJAX, reload js in #attached 3 times I have a simple form api ajax which does nothing special. In the form I have a js file:

 $form['#attached']['library'][] = 'commerce/cart';


In the js file I put a log console:

function ($, Drupal) {
  Drupal.behaviors.cart = {
    attach: function (context, settings) {
      console.log('aaaa');
  }
});


The log console prints 3 times: "aaaa" after the ajax action is over.
As if the file was loaded 3 times.
If you load the page normally, without ajax, the script runs one time
The log console prints 3 times: "aaaa" after the ajax action is over


That's correct, behaviours are re-attached after AJAX requests. That's actually the point of them - to ensure that Drupal modules can respond to changes in the DOM.

If you want to limit the effect of that, core provides once(). You'll need to add the once library separately, after which you can use, for example:

$(context)
  .find('input.myCustomBehavior')
  .once('myCustomBehavior')
  .addClass('processed');


See the JavaScript API overview for more information.

Of course you don't have to use once, if you don't need the complexity it offers you can just keep track of a boolean that determines whether or not your code should execute again.