How to use jQuery Plugin with Angular 5 and TypeScript 2.8.1?

420 views Asked by At

I want to import jQuery in an Angular 5 project using TypeScript 2.8.1. I follow Ervin Llojku solution but this does not the trick :

Install jquery with npm

npm install --save jquery

Install jquery with npm

npm install --save-dev @types/jquery

Add scripts to .angular-cli.json

"apps": [{
  ...
  "scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
  ],
  ...
}]

I also try to add these imports in app.module.ts or in component :

import * as $ 'jquery';

Whatever I try TypeScript compiler throws this error :

Error:(7, 15) TS2497: Module ''jquery'' resolves to a non-module entity and cannot be imported using this construct.
1

There are 1 answers

0
Bludwarf On

I have followed another answer and I replaced

import * as $ 'jquery';

with

import 'jquery';

in app.module.ts and everything seems to be OK.

Still I don't know if this error was due to Angular 5 or TypeScript 2.8.1...

or maybe to the tsconfig.json generated by Angular :

"compilerOptions": {
  "moduleResolution": "node",
}