Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

javascript - require is not defined error with browserify

I'm new to browserify and trying to load npm modules in browser but I'm getting the following error:

Uncaught ReferenceError: require is not defined

I'm following the tutorial from http://browserify.org/. Created javascript file with the following content:

var unique = require('uniq');

then run

npm install uniq

and

browserify main.js -o bundle.js

the bundle.js file is generated and I included it in my html but still getting the above error. Any ideas what am I doing wrong?

This is the content of final HTML file:

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <script src="bundle.js"></script>
    <script src="script.js"></script>
</head>
<body>

</body>
</html>

This is the content of bundle.js: http://pastebin.com/1ECkBceB

and this is script.js:

var unique = require('uniq');

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The "require" function is only available in the "bundle.js" script context. Browserify will take all the script files necessary and put them into the "bundle.js" file, so you should only have to include "bundle.js" in the HTML file, not the "script.js" file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...