12

In my Angular-12 project I installed file-saver:

npm install [email protected]

npm install --save exceljs

ng generate service services/export-excel

Then in the export-excel service, I added:

import * as fs from 'file-saver';

I got file-saver highlighted

with this error:

Could not find a declaration file for module 'file-saver'. 'c:/xampp/htdocs/myapp/node_modules/file-saver/dist/FileSaver.min.js' implicitly has an 'any' type. Try npm i --save-dev @types/file-saver if it exists or add a new declaration (.d.ts) file containing declare module 'file-saver';

But I didn't use:

@types/file-saver

How do I get this error resolved?

Thanks

3
  • @types/file-saver is used by for tslint of typesctipt itself. so it is better to install it when installing a library. So you can now just try npm i --save-dev @types/file-saver Commented Sep 17, 2021 at 14:03
  • @VishnuVinod - So, it means I will now have two (2) file-saver Commented Sep 17, 2021 at 14:59
  • Not like that. The @types/file-saver is like an optional dependency for file-saver when it is using with typescript. so you wll get code suggestions and and auto-completions from file-saver library Commented Sep 17, 2021 at 20:43

3 Answers 3

20

Additional typescript definitions

npm install @types/file-saver --save-dev

Sign up to request clarification or add additional context in comments.

Comments

8

I just solved a similar error with the same error message (I am working currently in a Angular 13 proyect), there is a solution first you will have to create a folder called 'types' in the same folder level of src:

create 'types' folder in the same level of src

then inside that folder create another folder with your module name 'file-saver' in this case and inside there a file called 'index.d.ts'

The final result need to look something like:

enter image description here

Last step is to put inside index.d.ts the following code:

declare module 'file-saver';

1 Comment

Works in Angular 15 too.
0

Add

"file-saver": "~2.0.5"

to the dependencies section in the package.json file, and remove it from the devDependencies section.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.