Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
Javascipt loaded file dynamically but could not access constructor. X is not a constructor

I'm trying to get rid off import statements but want to dynamically load Javascript files.

I'm using this class to do it.

export function JSImport<T>(
    importPromise: Promise<T>,
): Promise<T> {
    // CommonJS's `module.exports` is wrapped as `default` in ESModule.
    return importPromise.then((m: any) => (m.default || m) as T);
}

Anyway, when imported file has used to be with constructor parameters, I get error.

Here is the old usage (which works well):

declare let Tiff: any;
const tiff = new Tiff({buffer: xhr.response});

Here is the new usage:

   const importTiff = JSImport(
    import(/* webpackChunkName: "Tiff" */ 'Tiff'),
    );



   try {
    importTiff.then((Tiff: any) => {
    const tiff = new Tiff({buffer: xhr.response});
    ....
    }

In new codeline, I get error which says that TypeError: Tiff is not a constructor

Tiff variable looks like this in Chrome console. enter image description here

How to use it as a constructor ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The webpack dynamic import import(/* chunkNameHint */ "moduleName") really gives a module, that you identify as generic T in your JSImport function:

export function JSImport<T>(
    importPromise: Promise<T>, // TypeScript infers T to be the output of the dynamic import, i.e. the full module
): Promise<T>

Instead of trying to automatically "unpack" the module (which may have a default and/or named exports), you can direclty use the dynamic import:

import(/* webpackChunkName: "Tiff" */ 'Tiff')
  .then((module) => module.default) // Assuming the module has a default export and it is the one you need
  .then((Tiff: any) => {
    const tiff = new Tiff({buffer: xhr.response});
    // ....
   });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!