2

I have 2 files: bot.js and queue.js In the second file i define a variable called queue using this code:

const { google } = require("googleapis");
const play = require('play-dl');
const lista = []

module.exports = { lista }

module.exports = {...
}

In the first file i try to import the object with this code:

require(`./commands/queue.js`)
console.log(lista)

But when And i would like to be able to use its content in the first file How can i do that? I've seen other answers and tried their methods but couldn't make it work

7
  • you'll have to do module.exports = { lista } in the second file and import it in the first file using require("queue.js") , if you get an import error, swap out queue.js with it's relative path Commented Sep 30, 2021 at 1:32
  • The module.exports = { lista } should be added above the existing one? and once i add require("queue.js") to the first file, doing console.log(lista) would show the contents of lista on the console? Commented Sep 30, 2021 at 1:34
  • assuming lista is the object that you want to export, yes. Commented Sep 30, 2021 at 1:35
  • I just get undefined Commented Sep 30, 2021 at 1:45
  • 1
    the answer has the correct syntax, please accept it if it has solved your problem Commented Sep 30, 2021 at 11:27

1 Answer 1

1

Ensure you export the object, then require it by deconstructing from the exports

Queue.js

const { google } = require("googleapis");
const play = require('play-dl');
const lista = []

module.exports = {
   lista
}

Bot.js

const { lista } = require('path-to-queuejs-here');

console.log(lista)
// []
Sign up to request clarification or add additional context in comments.

Comments

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.