首页 > 解决方案 > fs.createReadStream 未定义

问题描述

所以最初我试图使用谷歌云节点客户端来做一些事情。然后错误

Unhandled Rejection (TypeError): fs.createReadStream is not a function

继续出现。

这是导致此问题的“google-auth-library”中的代码片段

const fs = require("fs");
...
const filePath = path.resolve(this.keyFilename);
const stream = fs.createReadStream(filePath);
await this.fromStreamAsync(stream, this.clientOptions);

我试图打印出 fs 和 fs.createReadStream,它显示 fs 是一个对象,而 createReadStream 的类型是未定义的。

const fs = require("fs");

console.log( "createReadStream type...");
console.log( typeof fs.createReadStream );

节点版本是 v12.13.1,我正在 Chrome 上运行一个由 create-react-app 初始化的 react 应用程序。

谁能告诉我如何解决这个问题?

标签: node.jsreactjsgoogle-cloud-platformfs

解决方案


您必须导入所有库或模块并正确定义 filePath。

试试下面的代码:

 var fs      = require('fs'),
 path    = require('path'),
 url     = require('url');

 var fileUrl = 'http://beta.business.applane.com/rest/file/download'+ '?token=5437bdb1811dfb0f03ec49a3&filekey=541bc4c909e41ce549567c07';

 var filename,
 filename = path.basename(url.parse(fileUrl).path);

 console.log(filename);
 const t = fs.createWriteStream(path.join(__dirname, filename));
 console.log(t)

推荐阅读