How to load a JavaScript on demand using RequireJS

Step 1

Create requirejs-config.js file and specify a dinamically loaded library location

Dfe/Markdown/view/adminhtml/requirejs-config.js:

var config = {
	paths: {
		Dfe_Markdown_SimpleMDE:  './Dfe_Markdown/SimpleMDE/simplemde.debug'
	}
}; 

Step 2

Place the on-demand loaded script into Dfe/Markdown/view/adminhtml/web/SimpleMDE/simplemde.debug.js

Step 3

Use RequireJs:

require(['jquery', 'Dfe_Markdown_SimpleMDE'], function($, SimpleMDE) {$(function() {
	console.log(SimpleMDE);
});});

See also:

An example of absolute (external) URLs in the requirejs-config.js file (from my Checkout.com payment extension):

https://github.com/CKOTech/checkout-magento2-plugin/blob/1.0.31/view/frontend/requirejs-config.js?ts=4#L3-L15

1 Like

Don’t we use map: instead of paths?

Who is «we»?
The code above is a really working code from my Checkout.com payment extension, so I can ensure I do use paths and do not use map.