【问题标题】:How to import a module that is in a different folder in Node.js?如何导入位于 Node.js 中不同文件夹中的模块?
【发布时间】:2021-02-18 06:49:20
【问题描述】:

亲切的问候,希望你一切都好。

我想要一些关于如何导入另一个文件夹中的模块的指导。

这是一张图片:

但是在导入模块时它会产生错误:

Error: Cannot find module '../controllers/index.js'
Require stack:

非常感谢您的指导。

【问题讨论】:

  • ../../controllers/index.js

标签: javascript node.js backend ecma


【解决方案1】:

您需要了解路径是如何工作的。您的路径中又缺少一个目录跳过。

const module = require('../../controllers/index.js'); //as you are in route module, you need to go back to controller directory(so ../../path of file)

更多路径详情可以查看here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多