How to add javascript with #attached in a node which contains a specific paragraph bundle I need to attach js in all nodes with a specif paragraph bundle. I know how to do this with drupal_add_js, but I prefer don't use it to prevent cache problems.

I am trying with hook preprocess_entity but I don't get it, I don't know how to use the ["#attached"]

function THEME_preprocess_entity(&amp;$variables) { 
  if (isset($variables['paragraphs_item']-&gt;bundle) &amp;&amp; variables['paragraphs_item']-&gt;bundle == "XXXXX") {
   $variables["elements"]["#attached"]['js'][] = array(
                'type' =&gt; 'file',
                'data' =&gt; 'myfile.js',
              );
      }
    }


What am I doing wrong?
Try this code for attaching

$variables['#attached']['js'][] = 'myfile.js';


Or

$variables['#attached']['library'][] = 'myfile.js';