How to get url for static image file in Magento 2's javascript files?

jurgen picture jurgen · Apr 27, 2016 · Viewed 10.2k times · Source

I need to get a URL for an image stored in my theme (app/design/frontend/MyVendor/MyTheme/web/images/image.png) from a javascript file (payment.js).

In PHP I can do it like this:

<?php echo $block->getViewFileUrl('images/image.png') ?>

How can I do this in JavaScript?

Answer

Holly picture Holly · Nov 10, 2019

In JavaScript it's available with require.toUrl('images/image.png').