2016-11-19 105 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); 

您需要模塊與「..」

相關問題