1

Does someone knows why it doesn't work ?

app/code/Vendor/Module/view/frontend/requirejs-config.js

var config = {
    map: {
        '*': {
            module: 'Vendor_module/js/go',
        }
    }
};
console.log('foo'); // i get well "foo" in console

app/code/Vendor/Module/view/frontend/web/js/go.js

define(['jquery'], function($){
   "use strict";
       return function hello()
       {
           alert('hello from function');
       }    
});

app/code/Vendor/Module/view/frontend/templates/file.phtml

<script type="text/javascript">
    require(['jquery', 'module'], function($, hello) {
        hello();
    });
</script>
4

I think you need to delete var/view_preprocessed and run a php bin/magento setup:static-content:deploy (or deploy static assets via your workflow) as it works fine for me.

I wouldn't recommend running JS directly in a PHTML template though, the best practice is to use either data-mage-init or x-magento-init as described in the Dev Docs here.

enter image description here

  • Thanks for your advice but still the same problem, i deployed the static-content even im in developer mode, basically we dont need that in developer mode, and i clean the cache but nothing happen ! – PЯINCƏ Mar 2 '18 at 17:14
  • In your PHTML file if you add console.log(hello); what is the output? – Ben Crook Mar 3 '18 at 12:43
  • Inside require(['jquery', 'module'], function($, hello) { ... I dont get it, in outside that something like this: <script type="text/javascript"> console.log('hello'); </script> i get it – PЯINCƏ Mar 3 '18 at 12:56
  • 1
    Ah okey, thank you for you and especially for your time. I added you in linkdin. – PЯINCƏ Mar 3 '18 at 14:35
  • 1
    I just asked this question if you want to answer magento.stackexchange.com/questions/218896/… – PЯINCƏ Mar 19 '18 at 13:24
0

I always use the following layout in the requirejs-config.js file:

var config = {
map: {
    '*': {
        module: "Vendor_module/js/go",
    }
},

shim: {
    'module': {
        deps: ['jquery']
    }
},

deps: [
    "js/go"
]
};
  • The same issue, now when you added deps: [ "js/go" ] i still have a new error : Error: Script error for: js/go and a warning fail loading js/go.js – PЯINCƏ Mar 2 '18 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.