Magento 2 – Get Base RequireJS BaseUrl for Static Files

javascriptmagento2

I am creating a custom module which retrieves the image from Package_Module/images/image.jpg in my javascript code. How to get the require js base URL or Static URL? or do you have any suggestions on what is the best to get the link of an image asset from javascript level?

Best Answer

You can pass it through using x-magento-init as Khoa mentioned, or you can use this:

require.toUrl('test');

Which will output:

//localhost:3000/static/frontend/Vendor/theme/en_GB/test

Where localhost:3000 is your base URL.