问题
I am trying example in http://browserify.org/ and try to make a function call as follows:
My html is:
<!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 is:
var unique = require('uniq');
var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];
console.log(unique(data));
function hello(){
alert("here");
}
I did browserify main.js -o bundle.js, so I can use require successfully.
But when I click the button, I have the error:
"Uncaught ReferenceError: hello is not defined"
Any suggestion will be appreciated!
回答1:
Browserifies primary purpose is to make JavaScript modules privately scoped so it has no way to see what you are trying to do.
Try using
global.hello = function() { alert("hello");}
See defining global variable for browserify.
In general, this is bad practice and you should instead export public properties out of your module and reference them via the required module reference.
来源:https://stackoverflow.com/questions/30522849/using-browserify-uncaught-referenceerror-function-is-not-defined