I'm trying to load a css file in a custom module, to style a template file I'm rendering. I followed the basic steps:

  version: 1.0
      css/rr_users.css: {}
    - rr_users/global-styling


name: RR users module
description: Generates editing pages for users
package: Custom
type: module
core_version_requirement: ^9
  - rr_users/global-styling

I can't find out why this isn't working. It looks to me that I did it correctly. Also I already tried to look up a solution, but none of the found ones worked out for me.

I already tried to empty the cache etc.

2 Answers 2


libraries is not a valid key for a module .info.yml file; that key is for theme info files only. The full list of valid properties is available here.

There are a few different ways to attach libraries to a page from a module, depending on the requirement and available context. Those different methods are discussed, with examples, under Attaching a library to page(s) in the official docs.

Given the tags on the question, Attaching a library to a view might be of most interest to you:

* Implements hook_views_pre_render().
function yourmodule_views_pre_render(ViewExecutable $view) {
  if (isset($view) && ($view->storage->id() == 'super_awesome_view')) {
    $view->element['#attached']['library'][] = 'custom/custom_view';
  • Sadly that's not working either, I changed the yourmodule into rr_users and did the storage->id() check on 'user_dashboard, which is the name of the twig file. I also put the method within the .moduke, as the docs tell you to '
    – Jorn Reed
    Jun 13, 2022 at 11:27
  • I use the same approach all the time so it definitely works, make sure you've named everything correctly and are checking the right things (e.g. you should be testing against the view ID, not the template file name, in case those are different)
    – Clive
    Jun 13, 2022 at 11:43
  • Ah okay, I managed to get it to work. I misspelled rr_users.libaries.yml, but I also had to do ['#attached']['library'][] = 'custom/custom_view'; in my controller method that renders that view. Idk why, but with that double approach of mine it works now.
    – Jorn Reed
    Jun 13, 2022 at 11:46
  • You definitely don't have to add it twice (the system is setup to de-dupe anyway so it wouldn't do anything). Seems likely you don't have the IDs or something else quite right in the preprocess method if you're having to inject it somewhere else. But aside from some really pedantic arguments about where the code should be, adding in the controller won't cause any problems
    – Clive
    Jun 13, 2022 at 11:53
  • Alright, I currently have it like this function nn_users_preprocess_page(&$vars) { $vars['#attached']['library'][] = 'nn_users/global-scripting'; } which is in the .module file. Probably fine for now
    – Jorn Reed
    Jun 13, 2022 at 11:59

Adding assets (CSS, JS) to a Drupal module via *.libraries.yml / Attaching a library to all (or a subset of) pages shows that a module should implement hook_page_attachments() to attach its libraries to all the pages or more pages. It could also use a preprocess function, which is useful in the case the pages to which you want to attach the library use the same theme hook.

The method you are using is listed in Adding assets (CSS, JS) to a Drupal theme via *.libraries.yml / Attaching a library to all pages, which also says:

If you prefer attaching a library from within your module you can use hook_preprocess_HOOK() or hook_page_attachments().

  • The current version of drupal.org/docs/develop/creating-modules/… explicitly says that you can add the libraries key to the info.yml file. It does not seem to work for me, but that's what's documented. Nov 3, 2023 at 17:16
  • Only themes can reference their libraries in their .info.yml files; modules need to use hooks. This question is about a module, for which the answer is Use either hook_preprocess_HOOK() or hook_page_attachments().
    – apaderno
    Nov 4, 2023 at 13:35
  • Yes, it looks like that is a documentation bug. Nov 29, 2023 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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