最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: fs.createWriteStream is not a function in Node - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question asked Jun 30, 2022 at 8:51 davidnaglidavidnagli 7892 gold badges6 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

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

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();
}

发布评论

评论列表(0)

  1. 暂无评论