1- I tried to set an error message in Drupal 8 from javascript with the new Messages API using the following code:

const messages = new Drupal.Message();
messages.add('test message');

I get the following error on the console log:

drupal.js?v=8.8.5:13 Uncaught TypeError: Drupal.Message is not a constructor.

How can I include this API?

2- As I can see in message.es6.js, there is an option to set message type: error, warning and status. How to pass these params once I get the function working?

Reference Documentation: https://www.drupal.org/node/2930536

Thank you.

  • What does your .libraries.yml look like?
    – anonymous
    Commented Apr 29, 2020 at 8:57

1 Answer 1


You need to make sure your library declares a dependency on core/drupal.message, e.g.

    js/foo.js: {}
    - core/drupal.message

Once you've done that, you can choose the message type by including options:

const messages = new Drupal.Message();
messages.add('test message', { type: 'status' });

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.