使用browserify时,require未定义错误 [英] require is not defined error with browserify

查看:439
本文介绍了使用browserify时,require未定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是浏览器并尝试在浏览器中加载npm模块的新手,但我收到以下错误:

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


未捕获的ReferenceError :require未定义

Uncaught ReferenceError: require is not defined

我正在关注的教程http://browserify.org/ 。创建了包含以下内容的javascript文件:

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


var unique = require('uniq');

var unique = require('uniq');

然后运行


npm install uniq

npm install uniq


browserify main.js -o bundle.js

browserify main.js -o bundle.js

生成了bundle.js文件,我将其包含在我的html中,但仍然出现上述错误。任何想法我做错了什么?

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?

这是最终HTML文件的内容:

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>

这是bundle.js的内容:http://pastebin.com/1ECkBceB

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

这是script.js:

and this is script.js:


var unique = require('uniq');

var unique = require('uniq');


推荐答案

require功能仅在bundle.js脚本上下文中可用。 Browserify将获取所有必需的脚本文件并将它们放入bundle.js文件中,因此您只需在HTML文件中包含bundle.js,而不是script.js文件。

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.

这篇关于使用browserify时,require未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆