Reputation: 167
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
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
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