How to Add Custom JS File in Magento 2 Admin Panel
Magento 2 uses the requireJS library, i.e., javascript file loader, to work with JS files. The developers may, however, require to use custom Javascript for additional features not offered by the default Magento 2.
For example, a custom module requires functionalities such as to select an image, crop an image, display charts, etc. There are many features that can be implemented using Javascript and improve the Magento 2 admin panel. You can also add a custom Phtml file in Magento 2 admin panel to create new layout.
However, to do so, you need to follow the below steps to add custom JS file in Magento 2 admin panel:
Steps to Add Custom JS File in Magento 2 Admin Panel:
- Create app/code/Vendor/Module/view/adminhtml/requirejs-config.js file with the following code:
12345678910var config = {paths: {'js-file-alias': 'Vendor_Module/js/js-file-name'},shim: {'js-file-alias': {deps: ['jquery']}}}; - Upload the JavaScript file at app/code/Vendor/Module/view/adminhtml/web/js/
- Put the following code in your .phtml file to load the JavaScript file
12345<script>requirejs(['jquery', 'js-file-alias'], function($){// Your Code});</script> - Run the following commands:
1php bin/magento setup:static-content:deploy -f1php bin/magento cache:clean
Note: Don’t forget to replace Vendor, Module, js-file-alias, and js-file-name with their respective values.
Follow these steps to add Javascript file in the admin panel for Magento 2. In the same way, you can also update total using JS in Magento 2.
I’d like to know what functionality you added using this method in the Comments section below. Also, please mention any doubts on the topic and I’d be happy to help.
Feel free to post the solution on social media and share it with fellow developers.
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Can I create a script to pull order data from a google sheet and create order in magento?
Hello Utkarsh,
As of now. Magento doesn’t have the ‘Order Import’ feature by default.
You can use any third-party extension to satisfy your requirements.
Thank you.
Can i use this way to put custom js for frontend?
Hello,
Yes, you can.
But make sure that you use the correct path for the front end.
Thanks.