Search code examples
javascriptnode.jsjsondiscord.jsconstants

How to load a json file depending on a variable


I have a trivia game in discord.js and need to be able to fetch questions and answers from a json file from a random number,

This is required because i have multiple lists of questions that it would pick based off like 1.json, 2.json based of a random number gen but every time i try to fetch a question it says that trivia is not defined even when it is.


Solution

  • require takes a string input, meaning the file path needs to be in quotes.

    const trivia = require('./game/trivia/1.json');