1
votes

If I have a number of responses: const exampleResponses = [ "name1", "name2", "name3"];

But let's say it's thousands of responses, not just three. Is there a way to pull in the responses from another file? As it stands, with thousands of responses it just clogs up my coding file.

3
Don't you have to get them into a file first? Please revise to add detail about your project. - isherwood

3 Answers

2
votes

A way to do this is to make a JSON file and call from it with JavaScript.

const { exampleResponses } = require('./example.json')
{
  "exampleResponses": [
    "name1",
    "name2",
    "name3"
  ]
}
2
votes

There are many ways you can go about this. One way would be to have a separate file with one array containing all the responses, then export that variable and require it in your main file

Responses.js

const responses = ['r1', 'r2', 'r3'] 
// And so on...

module.exports = {
   responses
}

Main File

const { responses } = require('filePathToResponsesJS')
1
votes

You can import the FileSystem module and do something like this:

responses= JSON.parse( fs.readFileSync( 'data/responses.json', function( err, data ) { ... }

Similarly, you can write to said file to keep things neat and tidy.