Home > front end >  How to import 'into-stream' package?
How to import 'into-stream' package?

Time:06-07

Code:

const express = require('express');
const app = express();
const cors = require('cors');
const mongoose = require('mongoose');
const multer = require('multer');
const inMemoryStorage = multer.memoryStorage();
const uploadStrategy = multer({ storage: inMemoryStorage }).single('image');
const { BlockBlobClient } = require('@azure/storage-blob');
const getStream = require('into-stream');

I can't use require in importing 'into-stream' module, it gives me ESM error:

Error [ERR_REQUIRE_ESM]: require() of ES Module

I tried adding type:module in package.json however require will not work if I do that. Do I install another version of into-stream? or should I use import rather than require. I am using node.js.

CodePudding user response:

The package doesn't support require() anymore, just like node-fetch mentioned at this thread:

Error [ERR_REQUIRE_ESM]: require() of ES Module not supported

You should add type: "module" to your package.json and then use:

import getStream from "into-stream";

I suggest you put that in the top of your file, just for code style :x

If this doesn't work, downgrade to version 6.0.0, which works with require(), according to the docs: https://www.npmjs.com/package/into-stream/v/6.0.0

  • Related