I have created a custom theme in app/design/frontend/Vendor/Themename
In theme directory I have Themename/requirejs-config.js
var config = {
 deps: [
 "js/custom",
 ]
};
Themename/web/js/custom.js
require([
 'jquery'
], function ($) {
 console.log('adsa');
 console.log(config);
});
I have a phtml file included and I am passing php variable to external js
<script type="text/x-magento-init">
{
 "*": {
 "js/custom": {
 "contacturl": "<?php echo $block->getUrl("contact/index/post"); ?>"
 }
 }
}
</script>
This is the error I am getting Config is not defined.
I am in the developer mode What I tried
Cleared Cache
Cleared Var Directory
2 Answers 2
I got this working using this
define([
 'jquery'
], function ($) {
 console.log('adsa');
 return function (config) {
 console.log(config.contacturl);
 }
});
Though I am not sure why its not working with require.
- 
 Hi, are you passing contacturl to custom js file? are you trying to call contacturl in custom.js? I want to do same can you tell me your purpouse of doing this?jack– jack2018年07月09日 13:17:55 +00:00Commented Jul 9, 2018 at 13:17
- 
 1Yes Jack I am passing the url to external js custom.js. The purpose for this is to use the url in js file generated by php.Amit Singh– Amit Singh2018年07月09日 13:34:11 +00:00Commented Jul 9, 2018 at 13:34
Although the OP has the answer, but I want to emphasize one thing here:
define([
 'jquery'
], function ($) {
 console.log('adsa');
 return function (config) {
 console.log(config.contacturl);
 }
});
In the above snippet, it MUST be "define"
If you use "require", you won't get what you want. And it's very hard to debug because there is no error.
One more thing, you can name the variable "config" above anything you want. It doesn't have to be exact word "config".
It can be "data", "request", "abc", ...
And from this variable, you can get what you put in "text/x-magento-init"
Explore related questions
See similar questions with these tags.
customJS code and I presuming that you are trying to passcontacturltocustomJS.