How can I avoid the output of my module is cached?
As Eyal mentioned, you can pass the variables from your module to jQuery like this:

mymodule.module

function mymodule_page_attachments(array &amp;$attachments) {
    $attachments['#attached']['library'][] = 'mymodule/auto';
    $attachments['#attached']['drupalSettings']['cool'] = 'hello world';     
  }
}


mymodule.libraries.yml

auto:
  version: 1.0
  js:
    js/auto.js: {}
  dependencies:
      - core/jquery


auto.js

(function($, Drupal, drupalSettings) {

    $( document ).ready(function() {
      alert(drupalSettings.cool);
    });

})(jQuery, Drupal, drupalSettings);


This will show a js alert message saying "hello world".