Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 FileSaver.js - Cannot find module 'file-saver'

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?

like image 967
gamelover42 Avatar asked Sep 23 '16 22:09

gamelover42


People also ask

Could not find a declaration file for module file saver in angular?

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 .

How do I use FileSaver in Angularjs?

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.

What is a file saver?

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.


1 Answers

just use this import statment

import * as fileSaver from 'file-saver';
like image 167
Andre Passos Avatar answered Sep 22 '22 22:09

Andre Passos