Using the browser function, the function "Do not copy ReferenceError: not defined"

I try the example at http://browserify.org/ and try to make a function call like this:

My html:

<!DOCTYPE html> <html> <head> <title>Test Browserify</title> <script src="bundle.js"></script> </head> <body> <button onclick="hello()">test</button> </body> </html> 

and my javascript:

 var unique = require('uniq'); var data = [1, 2, 2, 3, 4, 5, 5, 5, 6]; console.log(unique(data)); function hello(){ alert("here"); } 

I reviewed main.js -o bundle.js, so I can use it successfully.

But when I click the button, I have an error:

"Uncaught ReferenceError: hello undefined"

Any suggestion would be appreciated!

+6
source share
1 answer

Indicates that the main goal is to make JavaScript modules private, so it has no way to see what you are trying to do.

Try using

 global.hello = function() { alert("hello");} 

See the definition of a global variable for the browser .

In general, this is bad practice, and you should export public properties from your module and reference them using the required module reference.

+8
source

Source: https://habr.com/ru/post/988132/


All Articles