TypeError:FS.CreateWritestream不是节点中的功能

发布于 2025-02-11 18:25:51 字数 1229 浏览 1 评论 0 原文

我找不到任何资源在线解释为什么我遇到这个问题,即使官方节点js doc / api参考说createwritestream是fs中的一种方法:

fs.CreateWriteStream(路径[,options])[src]#

我发现的所有其他堆栈溢出问题都是不小心试图从浏览器访问FS的人。 因此,为了澄清,我正在使用节点!!

示例代码:

import fs from 'fs/promises'

let ws = fs.createWriteStream("example.png")

示例代码输出:

file:///workspace/buz/src/error.js:3
let ws = fs.createWriteStream("example.png")
            ^

TypeError: fs.createWriteStream is not a function
    at file:///workspace/buz/src/error.js:3:13
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

节点版本: v16.15.1
OS: Mac OS

I couldn't find any resources online explaining why I'm running into this issue, even the official Node JS docs / API reference say that createWriteStream is a method in fs:

fs.createWriteStream(path[, options])[src]#

All other stack overflow questions I found were people who were accidentally trying to access fs from the browser. So just to clarify, I am using Node!!

Example Code:

import fs from 'fs/promises'

let ws = fs.createWriteStream("example.png")

Example code output:

file:///workspace/buz/src/error.js:3
let ws = fs.createWriteStream("example.png")
            ^

TypeError: fs.createWriteStream is not a function
    at file:///workspace/buz/src/error.js:3:13
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Misc:

Node version: v16.15.1
OS: Mac OS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨巷深深 2025-02-18 18:25:51

我弄清楚了:

createWriteStream() fs 的函数,但不是 fs/promise !

我找不到任何文档在线上如何使用FS Prosiss使用 createWriteStream(),因此最简单的选择只是从基于正常回调的FS:

import fs from 'fs/promises'
import {createWriteStream} from 'fs'

let ws = fs.createWriteStream("example.png")

我读到节点文档通过 fs/Promises API中的所有方法,它们都没有返回< writestream> 。因此,到目前为止,如果没有从普通 fs api导入内容的情况下,似乎无法解决这个问题。

更新:在描述基于回调的FS API 时,更改措辞以不使用单词同步

I figured it out:

createWriteStream() is a function of fs, but not of fs/promises!!

I couldn't find ANY documentation online for how to use createWriteStream() with fs promises, so the easiest option is just to import it from the normal callback based fs:

import fs from 'fs/promises'
import {createWriteStream} from 'fs'

let ws = fs.createWriteStream("example.png")

I read through the Node documentation on fs and went through all the methods in the the fs/promises API and none of them return a <WriteStream>. So as of today, it seems like there's no way to get around this issue without importing stuff from the normal fs API.

Update: Changed wording to not use the word synchronous when describing the callback based fs API

宁愿没拥抱 2025-02-18 18:25:51

如果您想使用FS/Promise。首先,您应该打开文件以获取文件打架。然后,您可以使用fileHanlde.CreateWriteStream()。

import fs from 'fs/promises'

let file;
try{
   file = await fs.open("example.png");
   let ws = file.createWriteStream();
} finally(){
   await file?.close();
}

if you want use fs/promise. First, you should open file to get a FileHandle. then you can use fileHanlde.createWriteStream().

import fs from 'fs/promises'

let file;
try{
   file = await fs.open("example.png");
   let ws = file.createWriteStream();
} finally(){
   await file?.close();
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文