I am facing an issue setting up jquery in an angular 6 project. When I try to import it in the ts file, I encounter the following error:
Error: This module can only be referenced with ECMAScript imports/exports by turning on the 'allowSyntheticDefaultImports' flag and referencing its default export
The runtime error is:
Module '"/types/jquery"' resolves to a non-module entity and cannot be imported using this construct.
Here is my TypeScript code:
import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
@Component({
selector: 'app-anim',
templateUrl: './anim.component.html',
styleUrls: ['./anim.component.css']
})
export class AnimComponent implements OnInit {
constructor() { }
ngOnInit() {
$(function() {
$('.intro').addClass('go');
$('.reload').click(function() {
$('.intro').removeClass('go').delay(200).queue(function(next) {
$('.intro').addClass('go');
next();
});
});
});
}
}
I have also added allowdefault imports to my tsconfig.json as suggested by others. Here is my tsconfig:
{
"compileOnSave": false,
"compilerOptions": {
"paths": { "*": ["types/*"] },
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es5",
"allowSyntheticDefaultImports": true,
"typeRoots": [
"node_modules/@types"
],
Angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"textAnimation": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/textAnimation",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": ["./node_modules/jquery/dist/jquery.min.js"],
"es5BrowserSupport": true
},
I am struggling to understand what else I need to do to get jquery to work. Any help would be greatly appreciated. Thank you in advance.