imagekit-developer / imagekit-nodejs

Node.js SDK for ImageKit.io API.
https://imagekit.io
Other
84 stars 32 forks source link

TypeError: imagekit_javascript_1.default is not a constructor #72

Closed ohabash closed 11 months ago

ohabash commented 1 year ago

Im doing a trial on this api for my company

Ive got the following class in my app. but it wont run because of this error

typeError: imagekit_javascript_1.default is not a constructor

import ImageKit from 'imagekit-javascript';

export class ImagekitService {
  imagekit: ImageKit;

  constructor() {
    this.imagekit = this.client;
  }

  get client() {
    const creds = this.globalService.clientEnv.imageKit;
    return new ImageKit({
      publicKey: 'public_***,
      urlEndpoint: 'https://ik.imagekit.io/***'
    });
  }

  upload(file: File): Promise<string> {
    const self = this;
    const fileName = file.name;
    const uploadOptions: UploadOptions = {
      file,
      fileName,
      tags: ['tag1'],
    }
    return new Promise((resolve, reject) => {
      this.imagekit.upload(uploadOptions, function (err, result) {
        console.log(`🚀 => ImagekitService => upload => arguments:`, arguments);

        // error
        if (err) {
          console.log(`🚀 => ImagekitService => upload => err:`, err);
          return reject(err);
        }

        // get download url
        const url = self.imagekit.url({
          src: (result as any).url,
          transformation: [{ height: '300', width: '400' }],
        });
        console.log(`🚀 => ImagekitService => upload => url:`, url);

        // return download url
        return resolve(url);
      });
    });
  }
}

node => v16.19.0 npm => 8.19.3 imagekit-javascript => ^1.5.5

ohabash commented 1 year ago

Screenshot 2023-04-06 at 4 02 58 PM

blaueeiner commented 1 year ago

@ohabash Try add "esModuleInterop": true to your tsconfig.json

ohabash commented 1 year ago

That didnt work. This is what i have to do. @blaueeiner

import ImageKit from 'imagekit-javascript'; `// prod only
const ImageKit = require('imagekit-javascript'); // precompiled only
imagekitio commented 1 year ago

Here is the working code - https://stackblitz.com/edit/stackblitz-starters-ychubd?file=service.ts,index.ts

Can you please share the minimum code to reproduce this error for us to debug further?