我正在用node.js构建一个webapp并进行表达。Browserify不提供需求
我想使用Browserify使我的本地模块在浏览器中可用。
我的应用程序的结构是这样的:
├── app.js
├── lib
│ ├── controller
│ │ ├── home.js
│ │ └── mixer.js
│ ├── model
│ │ ├── command.js
│ │ ├── control.js
│ │ ├── room.js
│ │ └── user.js
│ └── view
│ ├── error.jade
│ ├── index.jade
│ ├── input.jade
│ ├── layout.jade
│ └── room.jade
├── package.json
└── public
├── images
├── scripts
│ ├── app.js
│ ├── command.js
│ ├── index.js
│ ├── room.js
│ └── user.js
└── styles
└── styles.css
我有一个名为Control
本地模块:
module.exports = function() {
var control = {};
control.command = false;
control.create = false;
control.last = 0;
control.room = false;
control.state = 2;
control.time = 0;
return control;
};
我用Browserify通过运行以下命令来创建一个文件public/scripts/app.js
:
browserify -d lib/controller/home.js > public/scripts/app.js
home.js
需要lib/model/user.js
这需要lib/model/control.js
,所以它应该在编译脚本中可用。
在我lib/views/layout.jade
文件,我打开这个脚本:
doctype html
html
head
title cosette #{name}
link(rel='stylesheet', href='http://cdn.jsdelivr.net/bootstrap/3.3.0/css/bootstrap.min.css')
link(rel='stylesheet', href='http://cdn.jsdelivr.net/bootstrap/3.3.0/css/bootstrap-theme.min.css')
link(rel='stylesheet', href='http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css')
link(rel='stylesheet', href='http://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.6.0/bootstrap-table.min.css')
script(src='http://code.jquery.com/jquery-1.11.1.js')
script(src='http://code.jquery.com/ui/1.11.2/jquery-ui.min.js')
script(src='http://cdn.jsdelivr.net/bootstrap/3.3.0/js/bootstrap.min.js')
script(src='http://cdn.socket.io/socket.io-1.2.0.js')
script(src='http://underscorejs.org/underscore-min.js')
script(src='/scripts/app.js')
link(rel='stylesheet', href='/styles/styles.css')
body
.container-fluid
.page-header
h1
a(href='/')#title cosette
a(href='/room/' + name).small#name= name
block content
然后,在我的lib/views/index.js
文件,我从public/scripts
导入的index.js
文件:
extends layout
block content
.row
.col-xs-12.col-sm-6.col-md-6
#notifications.list-group
.col-xs-12.col-sm-6.col-md-6
.input-group
input(type='text')#room.form-control
span.input-group-btn
button(type='button')#create.btn.btn-default Create
#rooms.list-group
// Activate page
script(src='/scripts/index.js')
最后,在我的public/scripts/index.js
文件,我尝试使用require
创建一个Control
实例:
var control = require('./lib/model/control.js');
var socket = io();
var rooms = {};
var update = function() {
$('#rooms').empty();
for (var roomIndex in rooms) {
var room = rooms[roomIndex];
$('#rooms').append('<a href="/room/' + room + '" class="list-group-item">' + room + '</a>');
}
socket.emit('control', control);
};
// Control
$('#create').click(function() { control.create = $('#room').val(); });
// Input
socket.on('rooms', function(message) { rooms = message; });
setInterval(update, 1000);
然而,当我试图加载这个页面,我得到一个错误:Uncaught ReferenceError: require is not defined
我已经看过在堆栈溢出或其他地方,在这个错误等问题,但一直没能解决这个问题。我该怎么办?
我的代码是可见到位桶:https://bitbucket.org/davidystephenson/cosette/src
的路径'/ lib目录/模型/ control.js'不从'公共/脚本/ index.js'存在。 – elclanrs 2015-03-18 23:32:26
那么我该如何导入我的控制模型? – 2015-03-18 23:59:33
'../../ lib/model/control.js'。你有没有浏览你的公共'index.js'? – elclanrs 2015-03-19 00:02:40