I am struggling to get FileSaver to work in my project. Here is the exact step I took
npm install filesaver.js --save
Inside app.component.ts
import * as FileSaver from 'filesaver.js';
Then I get this error
Cannot find module 'filesaver.js'.
I also tried to add the following to my angular-cli.json file
"scripts": [
"../node_modules/filesaver.js/FileSaver.min.js"
]
This is the closest that I could find that may help. However, this guy has a different setup as me. There's got to be a simple way to use this file right?
Note, after install FileSaver from npm, inside my node-module, I get filesaver.js folder (not sure why they have .js as folder name). Inside this folder, I get FileSaver.js and FileSaver.min.js
I have also tried
import * as FileSaver from 'filesaver.js/FileSaver.min.js';
But that just gives me
Cannot find module 'filesaver.js/FileSaver.min.js'.)
----update---
I have also tried typings file for this via
npm i @types/filesaver
and the result is the same
----update-----
Here is how I got it to work.
Added @types/filesaver and added the following code.
declare module 'filesaver' {
var saveAs: any;
export = saveAs;
}
Note. Is it bad that I am modifying content inside my node_module file. This means that every time a user run npm install, the custom code will not be there. How to get around it?
The default folder name downloaded from npm was filesaver.js. I changed it to filesaver. Why in the world would they name it as .js, the program seems to have trouble recognizing it as a folder
then import * as filesaver from 'filesaver'
then I can call
var blob = new Blob(['Some Content'], { type: "text/csv;charset=utf-8" });
filesaver.saveAs(blob, "hello world.csv");
----- folder structure-----
angular-cli: 1.0.0-beta.26 node: 6.9.1 os: darwin x64 @angular/common: 2.4.5 @angular/compiler: 2.4.5 @angular/core: 2.4.5 @angular/forms: 2.4.5 @angular/http: 2.4.5 @angular/material: 2.0.0-beta.1 @angular/platform-browser: 2.4.5 @angular/platform-browser-dynamic: 2.4.5 @angular/router: 3.4.5 @angular/compiler-cli: 2.4.5