How to add bootstrap.js in Magento 2?
Adding bootstrap.js in Magento 2 requires several steps to be done.
First of all, you should create the following module folder structure:
1 2 3 4 5 |
app / code / [Vendor] / [ModuleName] app / code / [Vendor] / [ModuleName] / etc app / code / [Vendor] / [ModuleName] / view / frontend / layout |
Then, it is necessary to create module files:
1 2 3 4 5 6 7 8 9 |
app / code / [Vendor] / [ModuleName] / registration.php app / code / [Vendor] / [ModuleName] / etc / module.xml app / code / [Vendor] / [ModuleName] / view / frontend / requirejs-config.js app / code / [Vendor] / [ModuleName] / view / frontend / layout / default.xml app / code / [Vendor] / [ModuleName] / view / frontend / layout / default_head_blocks.xml |
registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, '[Vendor]_[ModuleName]', __DIR__ ); |
module.xml
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/Module/etc/module.xsd"> <module name="[Vendor]_[ModuleName]" setup_version="0.0.1" /> </config> |
requirejs-config.js
1 2 3 4 5 6 7 8 9 10 |
var config = { paths:{ "jquery.bootstrap":"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min" }, shim:{ 'jquery.bootstrap':{ 'deps':['jquery'] } } }; |
default.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/Module/etc/module.xsd"> <referenceBlock name="head"> <block class="Magento\Theme\Block\Html\Head\Script" name="requirejs" before="-"> <arguments> <!-- RequireJs library enabled --> <argument name="file" xsi:type="string">requirejs/require.js</argument> </arguments> </block> <!-- Special block with necessary config is added on the page --> <block class="Magento\RequireJs\Block\Html\Head\Config" name="requirejs-config" after="requirejs"/> </referenceBlock> </page> |
default_head_blocks.xml
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/Module/etc/module.xsd"> <head> <css src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" src_type="url" /> <css src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" src_type="url" /> </head> </page> |
Now, you have to enable Module (SSH to Magento root):
1 2 3 4 |
php -f bin/magento module:enable --clear-static-content [Vendor]_[ModuleName] php -f bin/magento setup:upgrade |
And deploy static resources (SSH to Magento root):
1 2 |
php bin/magento setup:static-content:deploy |
Note that,
RequireJS will not load any JavaScript module source file until someone uses that JavaScript module as a dependency.
CMS Page example:
1 2 3 4 5 |
<script type="text/javascript"> requirejs(['jquery','jquery.bootstrap'], function(jQuery, jQueryBootstrap){ jQuery('.carousel').carousel(); }); </script> |
More tips from Magento 2 Cookbook