I am running Drupal 9.1.7 on lando on a Mac. I am writing a custom module and I want to include the Highcharts library. The .libraries.yml file looks like this.

    https://code.highcharts.com/highcharts.js: { type: external, minified: false }
    - core/jquery

From reading the documentation, I believe I should see the Highcharts library at the bottom of every page when I view the source, but I don't see it anywhere.

Am I missing something?

I have already validated the YAML content. I also installed the Charts module, if that makes any difference.


Defining the library is only half of the process. Libraries are not loaded on every page by default - you need to tell Drupal when it should include your library.

For full details, see the Attaching a library to page(s) section of the Adding stylesheets (CSS) and JavaScript (JS) to a Drupal module documentation on drupal.org.

I bet you want to attach the library to a render array:

$build['the_element_that_needs_asset_library']['#attached']['library'][] = 'your_module/library_name';

Or attach the library in a twig template (the template that produces your chart markup):

{{ attach_library('your_module/library_name') }}
  • Yes! I am trying to attach it to a render array like so: $chart['#attached']['library'][] = 'stats_degrees/highcharts'; $chart['#type'] = 'chart'; $chart['#chart_type'] = 'line'; $chart['#chart_library'] = 'highcharts'; etc. But when I return the array: return render($chart); it is always null and there is no Highcharts JS when I view source. This approach worked in D7 but D7 handled JS libraries differently and drupal_render() has been replaced by render(). Jun 30 '21 at 18:32
  • 1
    You usually shouldn't render a render array by calling render, you should just return it. That's the first thing that I would try. See drupal.org/docs/theming-drupal/twig-in-drupal/…
    – sonfd
    Jun 30 '21 at 19:07
  • I am doing it for debugging in PHP Storm, $rendered = \Drupal::service('renderer')->renderPlain($chart);. When I drill into the debugger I see that Renderer::executeInRenderContext() is returning null. There must be something amiss with my render array... maybe we should close this thread and I will open a new one after I do more debugging. Jul 1 '21 at 15:14
  • Yeah, that seems like a good idea. It sounds like you know how to attach the library properly, but there's a problem with your render array.
    – sonfd
    Jul 1 '21 at 23:38
  • I installed the highcharts library using composer. It installed it under /libraries. Makes sense. How do I reference this in my .libraries.yml file? I tried making it relative to my module ../../../libraries/highcharts/highcharts.js: { } but I still don't see it in the page source. I have tried attaching it to my render array and in a twig template like this: {{ attach_library('stats_degrees/highcharts') }} but I still don't see it. Jul 2 '21 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.