9

I have tried to create a Blob in Node.js. First just this:

var b = new Blob(['hi', 'constructing', 'a', 'blob']);

This fails with ReferenceError: Blob is not defined

Then I tried with the blob module (the two lines of code is from the example for this module, see https://www.npmjs.com/package/blob):

var Blob = require('blob');
var b = new Blob(['hi', 'constructing', 'a', 'blob']);

This fails with TypeError: Blob is not a constructor

How can I do it?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Leo
  • 4,136
  • 6
  • 48
  • 72
  • 1
    What version of Blob do you have installed? – bork Dec 05 '18 at 17:27
  • 1
    The package you refer to is for browsers, not Node.js From the description: *"A **cross-browser** `Blob` that falls back to `BlobBuilder` when appropriate. If neither is available, it exports `undefined`."* *(my emphasis)* – T.J. Crowder Dec 05 '18 at 17:27
  • 3
    What do you want to do with the blob? Node.js uses `Buffer` or typed arrays, not `Blob`s. – T.J. Crowder Dec 05 '18 at 17:27
  • 1
    If your using node.js, you might want to use Buffer instead. – Keith Dec 05 '18 at 17:29
  • @bork The latest. – Leo Dec 05 '18 at 18:02
  • @T.J.Crowder Are you sure? There is no "require" in the browser JavaScript. – Leo Dec 05 '18 at 18:04
  • @T.J.Crowder I catching the output from a system call and wants to upload it to some cloud storage. – Leo Dec 05 '18 at 18:10
  • 1
    @Leo - Yes, I'm sure. Somewhat confusingly, browser-targeted packages have been appearing (in droves) on `npm` the last few years. Modules using `require` have been a feature of bundlers like Webpack, Rollup, etc., for a while, and so people started using `npm` for common modules for browsers just like using it for common modules for Node.js. In fact, some modules are written to work in either environment. (But `blob` doesn't appear to be one of them.) – T.J. Crowder Dec 05 '18 at 18:12
  • 1
    @Leo - *"I catching the output from a system call and wants to upload it to some cloud storage."* I suggest deleting this question and instead posting a question asking how to do that. This is an example of [the X/Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) (which happens to us **all** sometimes!). You want to do X (upload to cloud storage), think you do that with Y (a blob), and so you've asked about Y. But Y isn't how you do that in Node.js. You want to ask about X. :-) – T.J. Crowder Dec 05 '18 at 18:13
  • 1
    But first I'd do some thorough searching, it seems like there's a lot of information out there for [uploading files with Node.js](https://duckduckgo.com/?q=upload+file+using+node.js&ia=web). – T.J. Crowder Dec 05 '18 at 18:16
  • @T.J.Crowder Thanks, I think there has been some valuable comments here so I do not think I will delete this question. But I will read a bit more about the thing I want to do and see if I need further help. – Leo Dec 05 '18 at 18:18
  • Do you use the Express framework? – T.J. Crowder Dec 05 '18 at 18:24
  • @T.J.Crowder No, I try to use more simple modules so I have a chance to know what is going on. – Leo Dec 05 '18 at 21:54
  • @T.J.Crowder I would gladly delete it if I could understand why. I had no idea that Blob was not supported in Node.js (and I still do not know why it is not supported). To me your comments have been very valuable. They are surely valuable to some other people too. Why delete this information? – Leo Dec 06 '18 at 11:24
  • 1
    @Leo - Comments are emphemeral on SO. They can and do go away without notice. But if you think the above is valuable, I'll collect it into an answer instead. :-) – T.J. Crowder Dec 06 '18 at 11:26
  • @T.J.Crowder I see. Please make your comments an answer then. – Leo Dec 06 '18 at 11:27
  • 1
    And you're quite right. "How do I use Blobs in Node.js?" is a perfectly reasonable question, to which the answer genuinely is "You don't, you use ___." Point well made. :-) – T.J. Crowder Dec 06 '18 at 11:33
  • 1
    Does this answer your question? [Node.js can´t create Blobs?](https://stackoverflow.com/questions/14653349/node-js-can%c2%b4t-create-blobs) – Richie Bendall Jan 04 '20 at 09:18

1 Answers1

13

Node.js has Blob support now, experimental since v15.7.0 / v14.18 and stable since v18.0.0, v16.17.0. This is part of the ongoing effort to bring web APIs to Node.js so code can be reused across the web and Node.js platforms.

The rest of this answer still applies re the blob package and the purpose you were going to use it for, though:


Node.js doesn't didn't used to have Blob, it uses Buffer (not to be confused with ArrayBuffer) and typed arrays.

The blob npm module you tried to use isn't for use in Node.js, it's for use in a browser, to smooth over historical differences in how you create Blobs in different browsers. From its description:

A cross-browser Blob that falls back to BlobBuilder when appropriate. If neither is available, it exports undefined.

(my emphasis)

Somewhat confusingly, browser-targeted packages have been appearing (in droves) on npm the last few years. Modules using require have been a feature of bundlers like Webpack, Rollup, etc., for a while, and so people started using npm for common modules for browsers just like using it for common modules for Node.js. In fact, some modules are written to work in either environment. (But blob doesn't appear to be one of them.)

In comments, you've said you want to upload a file from your Node.js process. You don't need a Blob for that, the way you do this in Node.js is different from the way you do it in a browser. So you probably want to research how to upload files from Node.js, without worrying about Blobs. That would be a different question, though. (One which may be answered here or, if you're willing to use Express, here.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875