node.js - 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 应用程序。
谁能告诉我如何解决这个问题?
解决方案
您必须导入所有库或模块并正确定义 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)
推荐阅读
- sqlite - 需要在 Ionic 4 的 SQLite 中存储文件/pdf
- java - Selenium,如何复制几个总是不同的单词?
- javascript - 如何过滤多维数组对象
- ruby-on-rails - Rails 5:如何从 API 的响应中保存 Array of Hash 中的 uniq 元素
- kubernetes - 这里实际节点的 CPU 和内存容量是多少?
- css - 如何制作浮动:相对位置下降?
- python - 如何加快我的python字典循环?
- git - 与推送相关的提交列表(远程)
- javascript - 单击另一个表格行时关闭上一个表格行
- python - 在python中合并CSV文件