【问题标题】:NODEJS Import config.js from parent folder into moduleNODEJS 将 config.js 从父文件夹导入模块
【发布时间】:2023-03-28 18:53:01
【问题描述】:

我正在尝试导入包含以下数据的配置文件:

module.exports = webhook_methods = {
GET : "GET",
POST : "POST",
PUT : "PUT"
}
module.exports = endpoints = {
WEBHOOKS : "webhooks",
ORDERS : "orders"
}

从父文件夹进入我的模块(webhook_module.js),结构如下

-server.js
-config.js
--webhook_module.js

在我的 webhook 模块中,我将配置导入为

const {webhook_methods,endpoints} = require('../configs');

并使用它来获取数据

_fecht_url = `https://${site}/api/${endpoints.WEBHOOKS}.json`;

但我得到的错误是端点和 webhook_methods 未定义,因此 require 不起作用,或者 export 不起作用。我想不通...

这类似于Import config.js file in another file,但在我的情况下,有两个不同之处我无法解决。我的 config.js 有很多导出,不只是一个,其次它位于父文件夹中。

配置文件是打字稿会更容易完成吗?我真的只想创建一个包含一堆枚举对象的配置文件并将其导入整个项目。如果这些枚举可以通过 VS Code 自动完成找到,那就太好了。

【问题讨论】:

  • 您正在重新分配module.exports。需要在上面设置一个属性exports.webhook_methods = {}exports.endpoints = {}

标签: node.js typescript visual-studio-code


【解决方案1】:

发现问题了,我在config.js中的导出应该是

module.exports.webhook_methods = {
GET : "GET",
POST : "POST",
PUT : "PUT"
}
module.exports.endpoints = {
WEBHOOKS : "webhooks",
ORDERS : "orders"
}

【讨论】:

    猜你喜欢
    • 2019-09-16
    • 2017-06-07
    • 1970-01-01
    相关资源
    最近更新 更多