What to write in module - Module not found: Error: Can't resolve 'xlsx' in angular

Discussão em 'Angular' iniciado por Ishan Malik, Fevereiro 18, 2025.

  1. Ishan Malik

    Ishan Malik Guest

    I'm trying to use xlsx library in my project but I'm continuously getting a following error


    Module not found: Error: Can't resolve 'xlsx'.
    ​

    I know, I have to declare in app.module but what would I declare here Here is my code; please guide me

    This is component file -

    import { Component, EventEmitter, OnInit } from "@angular/core";
    import { Router } from "@angular/router";
    import { FileUploader } from "ng2-file-upload";
    import * as XLSX from "xlsx";

    @Component({
    selector: "import-user",
    templateUrl: "import-user.component.html"
    })
    export class ImportUserComponent implements OnInit {

    public importResults: any[];
    public errorMessage: string;
    static reader: FileReader = new FileReader();

    ngOnInit(): void {

    }

    public uploader: FileUploader = new FileUploader({
    url: this.getUploadUrl(),
    disableMultipart: false,
    autoUpload: false,
    removeAfterUpload: false,
    method: 'post',
    allowedFileType: ['.xlsx', '.xls', '.csv'],
    });

    constructor(private router: Router) {
    }

    private initializeUploader() {
    this.uploader.onErrorItem = (item, response) => { this.errorMessage = response; };
    }

    public onFileSelected(event: EventEmitter<File[]>) {
    this.errorMessage = '';
    const file: File = event[0];
    console.log(file);
    }

    public getXLSXRequest(file: any){
    var wb = XLSX.readFile(file);
    }


    }

    This is module file

    import { FormsModule, ReactiveFormsModule } from "@angular/forms";
    import { BrowserModule } from '@angular/platform-browser';
    import { CommonModule } from "@angular/common";
    import { NgModule } from "@angular/core";
    import { routing } from "./routing";
    import { FileUploadModule } from "ng2-file-upload";
    import { ImportUserComponent } from "./components/import-user.component";

    @NgModule({
    imports: [
    CommonModule,
    routing,
    SharedModule,
    FormsModule,
    PortalModuleExports,
    FileUploadModule,
    BrowserModule
    ],
    declarations: [
    ImportUserComponent
    ],
    providers: [

    ]
    })
    export class AdministrationModule {
    }


    Here is the package as well.

    [​IMG]

    Continue reading...

Compartilhe esta Página