【问题标题】:config.json (TypeError: Only absolute URLs are supported)config.json(TypeError:仅支持绝对 URL)
【发布时间】:2020-09-13 08:43:55
【问题描述】:

我的 index.js 中有这段代码

const config = require('./config.json');

我的 config.json 的内容是

{
  "default_prefix": "?"
  }

但我收到一个错误:TypeError: Only absolute URLs are supported。是什么导致了错误?

如果我必须显示更多代码,请在 cmets 中询问我。

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    ./config.json 是一个相对 URL,它要求一个绝对 URL。

    尝试将其转换为绝对 URL:https://stackoverflow.com/a/38829194/2875073

    const configAbsolutePath = require('path').resolve('./config.json')
    const config = require(configAbsolutePath);
    

    或者您可以在此处尝试其他选项来读取 json 文件:Using Node.JS, how do I read a JSON file into (server) memory?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-20
      • 2019-01-02
      • 2023-02-22
      • 2019-08-14
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多