Can not find module 'xlsx' in angular

Jong-Hyen Kim picture Jong-Hyen Kim · Jan 23, 2018 · Viewed 14.2k times · Source

I want to import xlsx.

So, i install xlsx using npm install xlsx.

And import xlsx module. import * as XLSX from 'xlsx';

(Also I try import { foo, bar } from 'xlsx';)

And run this code with Angular cli. ng serve.

I use it.

Angular CLI: 1.6.1
Node: 6.11.3
OS: win32 x64
Angular: 2.4.10
... common, compiler, compiler-cli, core, forms, http
... platform-browser, platform-browser-dynamic

@angular/cli: 1.6.1
@angular/router: 3.4.10
@angular/tsc-wrapped: 0.5.2
@angular-devkit/build-optimizer: 0.0.36
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.34
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.1
@schematics/angular: 0.1.11
@schematics/schematics: 0.0.11
typescript: 2.0.10
webpack: 3.10.0

please help. thanks.

Answer

Pramod Patil picture Pramod Patil · Jan 23, 2018

This is not the way to add 3 party library which does not support import or ES6 functionality. Instead, you can add xlsx library in angular-cli.json file under script tag.

 // replace it with xlsx library
 "scripts": [
    "../node_modules/lodash/lodash.min.js"
  ],

And then in your component add below snippet and use it.

 import * as xlsx from "xlsx";