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>
asked Mar 2, 2018 at 12:08

2 Answers 2

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

answered Mar 2, 2018 at 17:04
16
  • 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 ! Commented Mar 2, 2018 at 17:14
  • In your PHTML file if you add console.log(hello); what is the output? Commented Mar 3, 2018 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 Commented Mar 3, 2018 at 12:56
  • 1
    Ah okey, thank you for you and especially for your time. I added you in linkdin. Commented Mar 3, 2018 at 14:35
  • 1
    I just asked this question if you want to answer magento.stackexchange.com/questions/218896/… Commented Mar 19, 2018 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"
]
};
answered Mar 2, 2018 at 15:31
1
  • 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 Commented Mar 2, 2018 at 17:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.