I am working on moving an angular 1.x site to a new angular 2.0 site I created the site using angular-cli 1.0.0-beta.15.
I have a button to export some data to a CSV file. When the page first loads I get an error message "Cannot find module 'file-saver'", but when I click the button everything works perfectly.
I have the FileSaver.js component installed:
package.json
...
"dependencies": {
...
"@types/filesaver": "0.0.30",
"file-saver": "^1.3.2"
...
}
...
in my export.service.ts:
import { saveAs } from 'file-saver';
...
let file = new Blob(['hello world'], { type: 'text/csv;charset=utf-8' });
saveAs(file, 'helloworld.csv');
...
Does anyone know how to resolve this error?
The error "Could not find declaration file for module" occurs when TypeScript cannot find the type declaration for a module. To solve the error, install the types for the module by running the command from the error message, e.g. npm install -D @types/module-name .
Basic usage Include ngFileSaver module into your project; Pass both FileSaver and Blob services as dependencies; Create a Blob object by passing an array with data as the first argument and an object with set of options as the second one: new Blob(['text'], { type: 'text/plain;charset=utf-8' }) ; Invoke FileSaver.
File Saver Overview. The File Saver component enables you to save files on the client machine. The saving of files is done through the saveAs method. You can consume the package from TypeScript and JavaScript projects alike.
just use this import statment
import * as fileSaver from 'file-saver';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With