Angular : Can't Export Excel Using Exceljs - Error Ts2307: Cannot Find Module 'stream' - Error Ts2503: Cannot Find Namespace 'nodejs'
I was try to export an excel file using ExcelJS Here is my console in VS Code terminal : ERROR in node_modules/exceljs/index.d.ts:1398:22 - error TS2307: Cannot find module 'stream
Solution 1:
In your tsconfig.app.json file add "types": ["node"]
Note, that the "types" are in the compilerOptions section of the tsconfig.
Solution 2:
You can't write directly a file on client side. that method is mean to be used on backend side in nodejs. if your expectation is download this file on client side. your code should be like :-
import { Component } from"@angular/core";
import * asExcelfrom"exceljs";
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
exportclassAppComponent {
title = "resttest10";
asyncdownloadExcel() {
const date = newDate()
.toISOString()
.slice(0, 10)
.split("-")
.reverse()
.join("/");
console.log(date);
const workbook = newExcel.Workbook();
const worksheet = workbook.addWorksheet("My Sheet");
worksheet.columns = [
{ header: "Id", key: "id", width: 10 },
{ header: "Name", key: "name", width: 32 },
{ header: "D.O.B.", key: "dob", width: 15 }
];
worksheet.addRow({ id: 1, name: "John Doe", dob: newDate(1970, 1, 1) });
worksheet.addRow({ id: 2, name: "Jane Doe", dob: newDate(1965, 1, 7) });
workbook.xlsx.writeBuffer().then((data: any) => {
console.log("buffer");
const blob = newBlob([data], {
type:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
});
let url = window.URL.createObjectURL(blob);
let a = document.createElement("a");
document.body.appendChild(a);
a.setAttribute("style", "display: none");
a.href = url;
a.download = "export.xlsx";
a.click();
window.URL.revokeObjectURL(url);
a.remove();
});
}
}
Solution 3:
I found a simple way to resolve this!
comment or remove the import import * as Excel from 'exceljs'
Now you can import like this:
import * as Excel from 'exceljs/dist/exceljs.min.js'
Solution 4:
while import the excel : try checking in tsconfig.app.json where in pre-defined code add "types": ["node"].
{"extends":"./tsconfig.json","compilerOptions":{"outDir":"./out-tsc/app","types":["node"]},"files":["src/main.ts","src/polyfills.ts"],"include":["src/**/*.d.ts"]}
Solution 5:
if you are using angular 8 and below, you need to install excelJs version 1.12.0
Post a Comment for "Angular : Can't Export Excel Using Exceljs - Error Ts2307: Cannot Find Module 'stream' - Error Ts2503: Cannot Find Namespace 'nodejs'"