简体   繁体   English

Config.js 文件无法导入变量

[英]Config.js file cannot import variables

I am making a large config.js file for all my messages (Discord.js V12).我正在为我的所有消息(Discord.js V12)制作一个大的 config.js 文件。 It works fine and everything, until the message I am trying to send contains variables (I am just stepping over from the situation with all variables spread, to one file).它工作正常,一切正常,直到我尝试发送的消息包含变量(我只是从所有变量传播到一个文件的情况中跳过)。 Somehow, the command file imports the variables from config.js, but not the other way around.不知何故,命令文件从 config.js 导入变量,但不是相反。 This leaves me in a very tricky spot, where the config.js file gets rendered pretty much useless (for my situation).这让我处于一个非常棘手的位置,config.js 文件变得非常无用(对于我的情况)。 I switched from an.ENV file to a.JS file, specifically so that (I thought) I can include variables in my external file.我从一个 .ENV 文件切换到一个 .JS 文件,特别是这样(我想)我可以在我的外部文件中包含变量。 Here is an sample for a problem:这是一个问题的示例:

Old situation:老情况:

const randomnumber = 5;

message.channel.send(`Hello world! ${randomnumber}`);

New situation, which doesn't work at the moment:新情况,目前不起作用:

const config = require("./../../Other/config.js");
const randomnumber = 5;

message.channel.send(config.messages.testmessage);

Here is a piece of config.js (the randomnumber variable doesn't work, which is my problem):这是一段 config.js( randomnumber变量不起作用,这是我的问题):

exports.messages = { 
  testmessage: `Hello world! ${randomnumber}`),
}

I think that it doesn't work if also place const randomnumber = 5 in the config.js file, since somethimes the value after const , also contains variables of previous constructed variables.认为如果在 config.js 文件中也放置const randomnumber = 5也不起作用,因为const之后的某些值还包含先前构造变量的变量。 Or maybe, it is possible, but I am not sure.或者也许,这是可能的,但我不确定。 So that's my question.所以这就是我的问题。

The randomnumber variable isn't shared between files, unless it is explicitly imported in the config.js. randomnumber变量不在文件之间共享,除非它在 config.js 中显式导入。 In order to use that variable, it either needs to be declared in the config.js:为了使用该变量,需要在 config.js 中声明它:

const randomnumber = 100;

exports.messages = {
  testmessage: `hello world: ${randomnumber}`
}

or, it can be shared using node imports and exports:或者,它可以使用节点导入和导出来共享:

exports.randomnumber = 100

config.js:配置.js:

const { randomnumber } = require('./otherfile.js');

exports.messages = {
  testmessage: `hello world: ${randomnumber}`
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM