How do I add a Javascript library to a field formatter?
In your formatter's viewElements

public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta =&gt; $item) {
  $elements[$delta] = array(
    '#theme' =&gt; 'theme_name_if_any',
    '#variablenametobepassed' =&gt; $this-&gt;viewValue($item) * 20, // any other preprocessing
    '#attached' =&gt; array('library'=&gt; array('module_name/library_name')),
  );
}

return $elements;
}