用browserify生成的js文件,在浏览器中引用后老报错,
Uncaught ReferenceError: require is not defined
不知道是什么原因。
错误信息指的是require
没有定义,说明你需要在bundle.js
外部使用require
,那么你生成的时候应该使用下面的方式生成。
external requires
You can just as easily create bundle that will export a require()
function so
you can require()
modules from another script tag. Here we'll create abundle.js
with the through
and duplexer modules.
$ browserify -r through -r duplexer -r ./my-file.js:my-module > bundle.js
Then in your page you can do:
<script src="bundle.js"></script>
<script>
var through = require('through');
var duplexer = require('duplexer');
var myModule = require('my-module');
/* ... */
</script>