marlokessler / strapi-plugin-image-optimizer

Optimize your images for desktop, tablet and mobile and different image formats.
MIT License
14 stars 10 forks source link

error: isImage is not a function #23

Closed AndreasFaust closed 1 year ago

AndreasFaust commented 1 year ago

Thank you for creating this module! Unfortunately I cannot get it to run. Following your instructions I end up receiving this error as soon as I try to upload an image:

TypeError: isImage is not a function
node_modules/@strapi/plugin-upload/server/services/upload.js:136:1

Strapi’s upload-Service tries to import isImage from plugin.services["image-manipulation"], but fails because it gets overridden by this plugin’s service, which does not contain isImage.

AndreasFaust commented 1 year ago

Fixed it. The problem was, that my Strapi installation was a bit older. It was updated to 4.11.7 but not to Typescript. I had to use require instead of import in ./src/extensions/upload/strapi-server.ts which was the problem. After updating Strapi to Typescript and using import it worked.

bogdaaamn commented 1 year ago

Is there a way to fix this without using import or switching to TypeScript? 😭

tudor-sv commented 7 months ago

I'm using Strapi 4.21.1, the JS, not TS version. I get the same error.

src/extensions/upload/strapi-server.js

const imageOptimizerService = require("strapi-plugin-image-optimizer/dist/server/services/image-optimizer-service");

module.exports = (plugin) => {
  plugin.services["image-manipulation"] = imageOptimizerService;
  return plugin;
};

I don't plan on switching to the TS version of Strapi. Is there a way to use this image optimization plugin in the js version of Strapi?

tudor-sv commented 7 months ago

PS: After some digging and debugging, I've figured out the cause of the problem:

const imageOptimizerService = require("strapi-plugin-image-optimizer/dist/server/services/image-optimizer-service").default;

module.exports = (plugin) => {
  plugin.services["image-manipulation"] = imageOptimizerService;
  return plugin;
};

Notice the .default at the end, when using require() It works now. Please add this to the docs ( "2. Extend Strapi's upload plugin" section ), in case others might bump into the same issue when figuring out how to import/require in the JS version of Strapi.

Thanks!