简体   繁体   English

Node.js fs.readfile无限工作

[英]Node.js fs.readfile works infinitly

I have a problem with a fs.readfile method. 我对fs.readfile方法有问题。 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. 我总是能在笔记本电脑上找到readFile上面的那一行,但是在大多数情况下(并非总是如此),readFile似乎一直在工作,它既不会记录任何错误,也不会运行授权方法。 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. 顺便说一句,我正在读取的.json文件为1kB。

You don't have to do fs.readFile on json files. 您无需对json文件执行fs.readFile。 Just require() the file and it will be available to you as parsed object. 只需require()文件,它就可以作为已解析对象使用。

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

console.log(file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM