Javascript 如何访问node.js子文件中的父模块数据

Javascript 如何访问node.js子文件中的父模块数据,javascript,node.js,Javascript,Node.js,我的文件就像这个结构的avilabele sample.js是根文件,test.js可保存在xx文件夹中 sample.js test.js 使用命令提示符运行test.js代码时: node test.js 它会产生如下错误: 找不到模块'./sample.js' 您需要父目录中带有“.”的模块。当您需要具有相对路径的文件时,它是相对于执行要求的文件的(请参阅)。在test.js中,require('./sample.js')将查找/path/to/xx/sample.js。使用requir

我的文件就像这个结构的avilabele

sample.js是根文件,test.js可保存在xx文件夹中

sample.js

test.js

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

node test.js

它会产生如下错误:

找不到模块'./sample.js'


您需要父目录中带有“.”的模块。

当您需要具有相对路径的文件时,它是相对于执行
要求的文件的(请参阅)。在
test.js
中,
require('./sample.js')
将查找
/path/to/xx/sample.js
。使用
require('../sample.js')
,因为它位于
test.js
的父文件夹中

    var name={
   h:14
}
module.exports=name;
var name=require('./sample.js')
console.log(name.h);
var name=require('../sample.js')
console.log(name.h);