I have been experimenting with integrating a JS library called pdfmake
into an Angular application. I found some guidance on how to accomplish this at https://www.freecodecamp.org/news/how-to-use-javascript-libraries-in-angular-2-apps/, which involved using the @types/pdfmake
package.
To install @types/pdfmake
, I used the following command:
npm install --save @types/pdfmake
In one of my components, I imported pdfmake like this:
import * as pdfMake from 'pdfmake/build/pdfmake';
import * as pdfFonts from 'pdfmake/build/vfs_fonts';
(<any>pdfMake).vfs = pdfFonts.pdfMake.vfs;
To leverage the types in pdfmake, I included the following import:
import * as _ from 'pdfmake';
However, upon doing so, I encountered this error:
This module can only be referenced with ECMAScript imports/exports by turning on the 'allowSyntheticDefaultImports' flag and referencing its default export.
In attempting to address this issue, I made adjustments to my tsconfig.json file as shown here:
{
"compileOnSave": false,
"allowSyntheticDefaultImports":true,
"esModuleInterop": true,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"module": "es2020",
"lib": [
"es2018",
"dom"
]
}
}
Despite these modifications, the error message regarding ECMAScript imports/exports persisted.
Edit:
I revised the tsconfig file to the following configuration:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"allowSyntheticDefaultImports":true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"module": "es2020",
"lib": [
"es2018",
"dom"
]
}
}
However, the error persisted even after this adjustment.