繁体   English   中英

node.js需要相对路径

[英]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等。

希望能帮助到你。

看看这个项目

https://github.com/nadav-dav/rekuire

它允许您使用“require”而不使用相对路径,只需说明文件/类名

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM