2016-11-19 73 views
0

我的文件就像avilabele这个结构。如何访问node.js中子文件中的父模块数据

enter image description here

sample.js是根文件和test.js是在XX夹即时拍摄。

sample.js

var name={ 
    h:14 
} 
module.exports=name; 

test.js

var name=require('./sample.js') 
console.log(name.h); 

当使用运行test.js代码命令提示:

节点test.js

它给错误,如:

找不到模块” ./sample.js'从父目录

回答

1

当您需要一个具有相对路径的文件时,它是相对于正在执行的文件e require(见here)。在test.jsrequire('./sample.js')将寻找/path/to/xx/sample.js。使用require('../sample.js'),因为它位于test.js的父文件夹中。

1
var name=require('../sample.js') 
console.log(name.h); 

您需要模块与“..”

相关问题