Tomek
Tomek

Reputation: 386

Node.js fs.readfile works infinitly

I have a problem with a fs.readfile method. I have the following code:

fs.readFile(path.join(__static, '/file.json'), function (err, data) {
        if (err) {
            console.log('Error loading client secret file: ' + err);

            return;
        }

        authorize(JSON.parse(data));
    });

The problem is, it worked fine all the time on my main computer but today I cloned my repo on the laptop and it does not work that well. I always reach the line above readFile occurence on laptop, but most of the times (not always), readFile seems to be working infinitly, it neither logs any errors nor runs authorize method. When I refresh my app and run this again, it reads the file instantly. This seems to me like the reading thread blocks itself for some reason, don't know why. And by the way, .json file I'm reading is 1kB.

Upvotes: 0

Views: 42

Answers (1)

Nikhil Ranjan
Nikhil Ranjan

Reputation: 992

You don't have to do fs.readFile on json files. Just require() the file and it will be available to you as parsed object.

let file = require('./file.json');

console.log(file)

Upvotes: 1

Related Questions