Append query parameter to module's javascript based on custom field on page
You probably want to approach this through hook_ENTITY_TYPE_view(). It's easy enough to get the current node in the page attachments hook, but as you only need to take action when a node is being viewed, using the more specific hook seems appropriate.

A basic example:

function my_module_node_view(array &amp;$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if ($view_mode == 'full' &amp;&amp; $entity-&gt;getType() == 'foo' &amp;&amp; $entity-&gt;hasField('field_bar')) {
    if ( ($val = $entity-&gt;field_bar-&gt;value) ) {
      $build['#attached']['library'][] = 'my_module/script';
      $build['#attached']['drupalSettings']['my_module']['script']['bar'] = $val;
    }
  }
}


The settings you add to the #attached array will be available in JS after a change to your libraries yml file to add a dependency:

script:
  js:
    ...
  dependencies:
    ...
    - core/drupalSettings


The value will be available in the JS var drupalSettings.my_module.script.bar.