0

I want to export a variable. but this happens

1st file

const commando = require('discord.js-commando');
const ytdl = require('ytdl-core');


class testCommand extends commando.Command {
    constructor(client) {
        super(client,{
            name: 'test',
            group: 'music',
            memberName: 'test',
            description: 'test',
        });

    }

    async run(message, args) {

        var Testo = 'hello'

    }

}

module.exports.Testo = Testo;
module.exports = testCommand;

2nd file

const commando = require('discord.js-commando');
const ytdl = require('ytdl-core');
var Testotest = require('./test.js')


class pauseCommand extends commando.Command {
    constructor(client) {
        super(client,{
            name: 'pause',
            group: 'music',
            memberName: 'pause',
            description: 'Pause music',
        });

    }


    async run(message, args) {

        message.channel.send(Testotest.Testo())

    }

}

module.exports = pauseCommand;

Error

ReferenceError: Testo is not defined
    at Object.<anonymous> (/Users/andrew/Desktop/NullBot_/commands/music/test.js:27:24)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/andrew/Desktop/NullBot_/commands/music/pause.js:3:17)
    at Module._compile (module.js:652:30)

Why does it give an error?

1
  • looks like you are instantly overriding exports.testo, try changing the last export line to something along these lines module.exports.testCommand = testCommand; Commented Sep 1, 2018 at 15:03

3 Answers 3

1

You define Testo in method run if you run method run Testo = 'hello', but you define class testCommand, so Testo is undefined, you shold run method run one time to define Testo.

This code

module.exports.Testo = Testo;

set module.exports = {Testo: Testo}

but you use

module.exports = testCommand;

set module.exports = testCommand

When you call Testotest.Testo is testCommand.Testo (undefined)

change your code in 1st file:

module.exports = testCommand;
module.exports.Testo = Testo;
Sign up to request clarification or add additional context in comments.

Comments

0

Your mayby gaonna wanna to use module.exports in this way.

module.exports = {
    key1: val1,
    key2: val2
}

So, your code module.exports.Testo = Testo; module.exports = testCommand; can use this format, and It will not throw errors.

2 Comments

what if I need to export a function that has to be run but I also need the function in a variable to be exported to be used in another file?
You can declear testCommand.Testo = Testo, and module.exports = testCommand;
0

You have this file defined, I understand is test.js:

const commando = require('discord.js-commando');
const ytdl = require('ytdl-core');


class testCommand extends commando.Command {
    constructor(client) {
        super(client,{
            name: 'test',
            group: 'music',
            memberName: 'test',
            description: 'test',
        });

    }

    async run(message, args) {
      var Testo = 'hello'
    }
}

// Testo is not defined because is under method run
module.exports.Testo = Testo;
module.exports = testCommand;

You can see the problem better now because is well indented. This module is loading sync and you are exporting Testo following sync way, so, the error is expected. If you want to fix this you need to define "var Testo" outside of "run" method or make this module async.

Regards

2 Comments

so is there a way to export a variable in a ‘run’ function?
without putting the module.exports inside the run function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.