【发布时间】:2013-07-03 15:46:15
【问题描述】:
我有一个具有以下结构的 Node.js 应用程序:
+
|-- app/
| |-- config.js
| |-- modules/ // MVC app modules/components.
| |-- login/
| |-- signup/
|-- lib/ // App specific modules/libraries.
| |-- auth/
| |-- storage/
|-- node_modules/ // 3rd party modules.
| |-- express/
| |-- hjs/
|-- public/
|-- app.js
|-- package.json
lib/ 内部的require 模块有哪些选项 - 例如 - 登录模块,但不必指定相对路径?
// app/modules/login/index.js
var auth = require('../../../lib/auth'); // <-- I'd rather have require('auth')
module.exports = function(app) {
app.get('/', auth.ensureAuthenticated, function(req, res) {
res.send('/');
});
};
我不想在 Github 上的 lib/ 中托管模块,并且希望只为第 3 方模块保留 *node_modules* 目录。
TJ 的Modular web applications with Node.js and Express(见 2:25)在谈到 bundledDependencies 时看起来很有希望。但是那个doesn't seem to work yet。
更新:
我现在已经稍微扁平化了我的结构,并决定改用相对路径。
【问题讨论】:
-
设置
$NODE_PATH是一个选项吗? -
如果有其他方法,我宁愿不这样做,因为在 Heroku 等人上没有设置它的选项。
-
你可以在(比如)
app/config.js中使用module.paths.push(__dirname + '/../lib/'),虽然感觉有点脏... -
感谢 Robert 抽出宝贵时间 :)
module.paths不久前不是被删除了吗?