如何解决如何从节点JS中的父文件夹中请求文件
我正在尝试使用父文件夹中文件中的var,但出现错误
这是我的文件夹和文件的结构:
Folder1
file1
Folder2
file2
这是我的代码(我正在尝试使用文件2中文件1中的var):
const fileOne = require('../folder1/file1');
我收到此错误:Error: Cannot find module '../folder1/file1'
解决方法
file1.js
var name = 'foobar';
exports.name = name;
file2.js
const { name } = require('../file1');
console.log(name); // prints foobar
然后如果您运行
$ node file2.js
foobar
基本上,
../file1
转到父文件夹并查找file1.js 确保您要从file1.js中获取变量,并按如下所示进行导入:
const { someVariable} = require('../fileName')'
,
也尝试:
const fileOne = require('../folder1/file1.js'); //
并确保您正确导出了它,
module.exports = whatever or { whatevers if there are more than 2} ;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。