NodeJs 要求,自定义模块,路径,
NodeJs require, custom module, path,
我想在下面寻求帮助。可能是重复的但没有找到解决方案。
我在 2 个不同的路径中有以下 2 个文件。
一个路径:Root/AppleRoot/Apple.js
B路径:Root/PeachRoot/Peach.js
在Peach.js;
module.exports.harvest = harvest;
function harvest(input, callback){
}
在 Apple.js 中,我想从 Peach.js
导入 harvest 函数
const harvestApple= require("../PeachRoot/Peach").harvest;
当我尝试 运行 代码时,它显示以下内容:
Error:
Unable to import module
如果我将 Peach.js 放在同一文件夹或 AppleRoot 下并更改路径,我可以毫无问题地导入模块。
我错过了什么?
必须有一种方法可以从不同的目录导入模块
如果你想在项目中使用某些东西,Node.js 必须知道它,所以要么在文件中使用完整路径目录,要么在你的 package.json 文件中声明它:
{
“name”: “harvestApp”,
“dependencies”: {
“Peach”: “file:/User/workspace/PeachRoot”,
“Apple”: “file:/User/workspace/AppleRoot”,
}
}
我想在下面寻求帮助。可能是重复的但没有找到解决方案。 我在 2 个不同的路径中有以下 2 个文件。 一个路径:Root/AppleRoot/Apple.js B路径:Root/PeachRoot/Peach.js
在Peach.js;
module.exports.harvest = harvest;
function harvest(input, callback){
}
在 Apple.js 中,我想从 Peach.js
导入 harvest 函数const harvestApple= require("../PeachRoot/Peach").harvest;
当我尝试 运行 代码时,它显示以下内容:
Error:
Unable to import module
如果我将 Peach.js 放在同一文件夹或 AppleRoot 下并更改路径,我可以毫无问题地导入模块。
我错过了什么? 必须有一种方法可以从不同的目录导入模块
如果你想在项目中使用某些东西,Node.js 必须知道它,所以要么在文件中使用完整路径目录,要么在你的 package.json 文件中声明它:
{
“name”: “harvestApp”,
“dependencies”: {
“Peach”: “file:/User/workspace/PeachRoot”,
“Apple”: “file:/User/workspace/AppleRoot”,
}
}