How do I add a JavaScript file to a form? I'm trying to add a JavaScript file to a form that I have in a modal.  Although I followed the information found here, here, and here, the file doesn't appear in the Network tab of the inspector and nothing is logged to the browser console when the modal is launched.

The form modal is triggered in
themes/custom/my_theme/templates/controllers/my-controller-theme.html.twig.

&lt;a class="use-ajax" data-dialog-type="modal" href="/help/{{ var1 }}/{{ var2 }}/add"&gt;Click here to add&lt;/a&gt;


The library is defined in my custom theme in themes/custom/my_theme/my_theme.libraries.yml.

my_modals:
  version: 1.x
  js:
    js/my_modals.js: {}
  dependencies:
    - core/jquery


The JavaScript file is themes/custom/my_theme/js/my_modals.js.

(function ($) {
    Drupal.behaviors.my_modals = {
        attach: function (context, settings) {
            console.log('test');
        }
    };
}(jQuery));


The form itself is defined in my custom profile in
profiles/my_profile/src/Form/MyModalAddForm.php.

public function buildForm(array $form, FormStateInterface $form_state) {
  // ...
  $form['#attached']['library'][] = 'my_theme/my_modals';
}
There is no use to attach libraries to a form that is displayed in a modal window because the window has no place for assets (JS and CSS files). Instead I would suggest using hook_page_attachements or attaching the library to page element that is responsible for triggering that modal window.