[英]node.js require relative paths
我在几个目录中有node.js模块,因为我遵循MVC模式。 我需要打电话要求到位于当前目录之外的几个模块。 我怎样才能做到这一点?
/app/controller/c1.js
...
/app/model/m1.js
...
/app/view/v1.js
...
/app/view/v2.js
// this works
require('./v2');
// these doesn't work
require('../model/m1.js');
require('~/model/m1.js');
...
为什么会这样?
对于其他目录中的模块,请使用以下格式:
testAuth =require('./public/javascripts/test.js'),
//in case the test.js is in the public/javascripts directory (I am using Linux)
如果你跳过'.js'扩展名,节点应该先找.js之前的.js等。
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.