简体   繁体   English

我无法访问其他脚本中的变量。 (Module.exports 并非在所有地方都有效)

[英]I cannot access the variable in the other script. (Module.exports don't work everywhere)

I have been having issues with module.exports .我一直遇到module.exports问题。
I have 2 Scripts that use Discord.js , and i need to have a SetTimeout() variable that is common to both scripts to be able to use ClearTimeout() from each of them.我有 2 个使用Discord.js的脚本,并且我需要有一个两个脚本通用的SetTimeout()变量才能从每个脚本中使用ClearTimeout()
I tried to use:我尝试使用:

//first script
var foo = 20;
module.exports.foo = foo;
//Second Script
var exp = require('./firstscript');
console.log(exp.foo);

This was a test, to see if i was doing it the wrong way, with a simple variable instead of my SetTimeout().这是一个测试,用一个简单的变量代替我的 SetTimeout(),看看我是否以错误的方式做这件事。
The test worked fine when i ran new scripts with the node command当我使用node命令运行新脚本时,测试运行良好
but with npm start on my 2 original scripts it returned 'undefined' while having the same syntax.但是在我的 2 个原始脚本上使用npm 开始,它返回“未定义”,同时具有相同的语法。

In every script is already a module.exports for the Discord.js event class like 'Ready' for exemple.在每个脚本中已经是Discord.js事件 class 的module.exports ,例如“Ready”。
I'm running this bit of code outside the main module.exports at the top where the const are declared.我在声明const的顶部的主module.exports之外运行这段代码。
I wonder if this is causing my issue.我想知道这是否导致了我的问题。

example:例子:

//the code i'm talking about is here.

module.exports = class ReadyEvent extends BaseEvent {
  constructor() {
    super('ready');
  }
  
  async run(client) {
 

Thanks for your help.谢谢你的帮助。
Ask me for clarification if needed.如果需要,请向我澄清。

EDIT:编辑:
I looked up on the internet to see if it was an issue with the module.export that was already present on the script.我在互联网上查找是否是脚本中已经存在的 module.export 的问题。 And it was.确实如此。

Apparently you cannot have several module.export in a script if each one of them doesn't specify a variable:显然,如果每个模块都没有指定变量,则脚本中不能有多个module.export

//this doesn't work
module.exports.value = value;
module.exports = value2;
//this works
module.exports.value = value;
module.exportd.value2 = value2;

My problem was that the one already present was used by the 'discord.js compiler' to register every command so i couldn't modify it without breaking the bot.我的问题是“discord.js 编译器”使用已经存在的那个来注册每个命令,所以我无法在不破坏机器人的情况下修改它。

I decided to put my timer in a new script named GlobalVars and it worked Perfectly fine.我决定将我的计时器放在一个名为 GlobalVars 的脚本中,它运行得非常好。
I'm Satisfied that it works now.我很满意它现在可以工作。
For me the issue is fixed but i would love to know if it is possible to export a variable WITH the discord.js module.exports syntax included.对我来说,问题已解决,但我很想知道是否可以使用 discord.js module.exports 语法导出变量。

From your Second Script variable, its seems to be requiring a folder and not a file, and I don't think folders are allowed.从您的Second Script变量来看,它似乎需要一个文件夹而不是文件,我认为不允许使用文件夹。

Assuming you are requiring a script and not a folder:假设您需要脚本而不是文件夹:

First Mistake: When requiring, inside the brackets you must have quotation marks ( "" ) or ( '' ).第一个错误:要求时,括号内必须有引号( "" )或( '' )。

If it is a folder, add a / next to ./firstscript and assign it to the file you want to refer to.如果是文件夹,在./firstscript旁边加一个/并将其分配给您要引用的文件。

var exp = require('./firstscript/THE_INTENDED_FILE');

... If firstscript is a json file: ...如果firstscriptjson文件:

var exp = require('./firstscript.json');

Extracting Variables from file从文件中提取变量

// Option 1:
console.log(exp.foo);
// Option 2:
const extract = exp.foo
console.log(extract);

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

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