cancel
Showing results for 
Search instead for 
Did you mean: 

How to override a java script file from an extension in your theme in Magento 2

SOLVED

How to override a java script file from an extension in your theme in Magento 2

So, I have a js file from an extension, that i need to override

 

the path is :  app/code/MyExtension/MyExtensionSubfolder/view/frontend/web/js/script.js

Where should i put the new js file, that will override script.js from above? And what else is necessary for this to work.

 

If I want to override a .phtml file from this extension, is simple. Create a new folder in

app/code/design/frontend/Venustheme/kasitoo  with the name combined MyExtension_MyExtensionSubfolder and then follow the path from extension and put here the phtml and it works.

 

But for js file from example if I create a new js file called script.js to the path

app/code/design/frontend/Venustheme/kasitoo/MyExtension_MyExtensionSubfolder/web/js/script.js is not working.

 

So the question is where should I put my new js file with the modifications I made, that will overide the script.js from the extension. Because if I made the changes directly in the extension, after the first update of the extension, I will lose those changes.

 

 

Tags (3)
2 REPLIES

Re: How to override a java script file from an extension in your theme in Magento 2

Hi @ralucaalbu,

 

Maybe this link could help: http://devdocs.magento.com/guides/v2.2/javascript-dev-guide/javascript/custom_js.html

--
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution"

Re: How to override a java script file from an extension in your theme in Magento 2

Hi!

 

Actually the magento documentation is not explained clearly from my point of view. Someone else responded to me on another site and the solution worked. I will put it here, maybe it will help others:

 

Create or modify app/design/frontend/yourStore/yourTheme/requirejs-config.js

var config = {
  "map": {
    "*": {
      "oldScriptAliasOrPath": "newScriptAliasOrPath",
      "MyExtension_MyExtensionSubfolder/js/script": "js/myExtensionCustom/script",
    }
  }
};

First one is a generic example and the second one is using your file as an example. Using this method means your JS file would be placed in app/design/frontend/yourStore/yourTheme/web/js/myExtensionCustom/script.js.