0

I am using laravel mix, which produces files correctly. Everything works fine, except when I call function form console I receive

Uncaught ReferenceError: test is not defined
at <anonymous>:1:1

This is part of code I get from running npm run dev. How can I call this function from console?

__webpack_require__(14);

function test() {
   return 'testing';
}

$(document).ready(function () {
   console.log(test());
});
Tim
  • 446
  • 1
  • 5
  • 17
  • Are you saying it doesn't work in your console.log(test())? Or are you saying console.log(test()) works, but then when you open the browser console and try test() that doesn't work? – frederickf Feb 27 '18 at 20:15
  • console.log(test()) works fine when page is loaded, but if I run this in console it doesn't. – Tim Feb 27 '18 at 20:38
  • Here is link to full .js after npm run dev: https://jsfiddle.net/hzg70cbu/ – Tim Feb 27 '18 at 20:42
  • 1
    Webpack wraps your code in functions to create scope, so that each file can act as an independent module. This keeps your various modules from polluting the global scope or each other and also means test is not available to the browser console. If you just want to call your test function from the browser console try assigning test to the window. – frederickf Feb 27 '18 at 20:59
  • That what I was thinking about. This way it works. Is there any other way? – Tim Feb 27 '18 at 21:08
  • Yes. Checkout https://stackoverflow.com/questions/37656592/define-global-variable-with-webpack – frederickf Feb 27 '18 at 21:20

0 Answers0