Linked Questions

Popular Questions

How to one-time initialize a node module with data from a local file

Asked by At

I am trying to create a node module that has some helper functions for searching through reference data that I have in a CSV file. I've used the csv-parser module for loading it into objects, and this API seems to be for use with an asynchronous stream reader / pipe situation. I don't want the helper functions in this module to be available to any other modules before this reference data has had a chance to load.

I've tried using a Promise, but in order to get it to work, I've had to expose that promise and the initialization function to the calling module(s), which is not ideal.

// refdata.js
const fs = require('fs');
const csv = require('csv-parser');

var theData = new Array();

function initRefdata() {
  return(new Promise(function(resolve, reject) {
    fs.createReadStream('refdata.csv')
      .pipe(csv())
      .on('data', function(data) {theData.push(data);})
      .on('end', resolve);}));
}

function helperFunction1(arg) { ... }

module.exports.initRefdata = initRefdata;
module.exports.helperFunction1 = helperFunction1;

// main.js
var refdata = require('./refdata.js');

function doWork() {
  console.log(refdata.helperFunction1('Italy'));
}

refdata.initRefdata().then(doWork);

This works for this one use of the reference data module, but it is frustrating that I cannot use an initialization function completely internally to refdata.js. When I do, the asynchronous call to the stream pipe is not complete before I start using the helper functions, which need all the data before they can be useful. Also, I do not want to re-load all the CSV data each time it is needed.

Related Questions