Discord Bot (node.js):从外部文件读取数据

Discord Bot (node.js) : read data from external file

我使用 node.js 设置了我的 discord BOT。为了我的利益,我需要将一些数据存储在外部文件中,但我似乎无法从我的 index.js 文件(主要 Bot 文件)访问它。 我试过在外部 js/json 文件中有一个静态数组,但我只能检索 undefined/empty 值。此外,当我尝试使用 .txt 文件时,一旦检索到内容,我发现它无法调用 string.split() 等函数。

我是不是漏掉了包裹里的东西?

假设您存储的数据采用 UTF-8 编码:

var fs = require('fs');

fs.readFile('path/to/file', 'utf8', function(err, contents) {
    // code using file data
});

假设没有错误,内容将是该文件中的数据字符串。

https://code-maven.com/reading-a-file-with-nodejs