Taimoor
Taimoor

Reputation: 167

Using nodejs fs module within my meteor app

I need to read some json file and then load the data into the db. Instead of using HTTP I want to use fs module from node.js. The question is that how can I use the following code from within meteor app.

var fs = require('fs');
var file = __dirname + '/test.json';

    fs.readFile(file, 'utf8', function (err, data) {
    if (err) {
         console.log('Error: ' + err);
         return;
    }

    data = JSON.parse(data);

    console.dir(data);
});

Upvotes: 0

Views: 1816

Answers (2)

Christian Fritz
Christian Fritz

Reputation: 21384

If you are within a package, then all you need to do is to replace the require with NPM.require:

var fs = Npm.require('fs');

If you want to use it in a project (not in a package), then just add the meteorhacks:npm to your project and then use

var fs = Meteor.npmRequire('fs');

Upvotes: 3

jusynth
jusynth

Reputation: 191

Or go the more Meteor way, so your code could begin with:

var data = Assets.getText('/test.json');
EJSON.parse(data);

Upvotes: 0

Related Questions