Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named export 'memoize' not found. The requested module 'lodash' is a CommonJS module // @imgly/background-removal-node #78

Open
Simon-Piquemal opened this issue Dec 4, 2023 · 15 comments
Assignees
Labels
bug Something isn't working

Comments

@Simon-Piquemal
Copy link

Hello, I get this error when I try to use the library:

file:///C:/Users/simon/BetterhostApi-1/node_modules/@imgly/background-removal-node/dist/index.mjs:402
import { memoize } from "lodash";
^^^^^^^
SyntaxError: Named export 'memoize' not found. The requested module 'lodash' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'lodash';
const { memoize } = pkg;

at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
at async loadESM (node:internal/process/esm_loader:34:7)
at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v21.1.0

@NewEpoch2020
Copy link

try const {removeBackground} = require("@imgly/background-removal-node");

@omatheusant
Copy link

i'm stuck with this same issue, you fix it? (i'm trying to use this library in a NextJs project)

@DanielHauschildt
Copy link
Contributor

We will check this out. Which version are you using?

@NewEpoch2020
Copy link

We will check this out. Which version are you using?
I use @imgly/background-removal-node 1.2.1, I encountered the same problem as him, and the problem was solved when I used "require".

@RobSchilderr
Copy link

We will check this out. Which version are you using?

Hey Daniel! I am also encountering this issue and would love to help you out for testing new versions.

I'm using version 1.2.1 with Next.js("13.5.6"). Initially, it appeared to work, but then I ran into the lodash issue as well. This problem also occurred with version 1.2.0. Any version below that doesn't work for me neither, because I love the .webp feature.

It seems to be a recurring issue with Next.js projects. Any insights or solutions would be greatly appreciated.

@DanielHauschildt
Copy link
Contributor

@RobSchilderr Can you send or post a zip that includes a project that can reproduce this error?

@DanielHauschildt DanielHauschildt self-assigned this Dec 24, 2023
@DanielHauschildt DanielHauschildt added the bug Something isn't working label Dec 24, 2023
@RobSchilderr
Copy link

@RobSchilderr Can you send or post a zip that includes a project that can reproduce this error?

I'd love to but don't have the time in the coming week, maybe the week later. Don't you have a test repo with Next.js? If you can confirm it works with Next.js then I have to debug the issue more deeply

@rikharink
Copy link

I'm having the same error in sveltekit (version 1.3.0 using the import from the readme). Attached is the zip from https://github.com/rikharink/pfp.toi.vet/tree/local_models that should reproduce this. (removed the modals and wasms from /static to keep the size of the zip small)
repro-lodash-error-sveltekit.zip

@RobSchilderr
Copy link

from

@DanielHauschildt is this enough or do you also need my example?

@KshitizPratap
Copy link

KshitizPratap commented Jan 19, 2024

@DanielHauschildt Facing the same issue in Nextjs. Here's the version of the package-
"@imgly/background-removal": "^1.4.0"

Is there a workaround for this?

@billyadelphia
Copy link

this works for me on nuxt

const tryRemoveBackground = async () => {
  tryRemoveBackgroundStatus.value = true;
  try {
    const imglyRemoveBackground = await import('@imgly/background-removal');
    const config = {
      output: {
        format: "image/png",
        quality: 1
      },
      progress: (key: string, current: number, total: number) => {
        console.log(`Downloading ${key}: ${current} of ${total}`);
      }
    }
    await imglyRemoveBackground.preload(config as any).then(() => {
      console.log("Asset preloading succeeded")
    })
    // imageBackgroundSrc.value = "https://bad.src/not/valid"
    await imglyRemoveBackground.default(imageChosen.value?.originalImageData.base64, config as any).then((blob: Blob) => {
      // result is a blob encoded as PNG.
      // It can be converted to an URL to be used as HTMLImage.src
      //convert blob to base64
      const reader = new FileReader();
      reader.readAsDataURL(blob);
      reader.onloadend = () => {
        const base64data = reader.result;
        imageBackgroundSrc.value = base64data as string;
      }
    })
  } catch (e) {
    console.log(e);
  }
  tryRemoveBackgroundStatus.value = false;
}

@Paquette1111
Copy link

Paquette1111 commented Feb 13, 2024

After reading the previous comments this was my work around in my sveltekit project. I imported lodash-es and updated the import in @imgly\background-removal\dist\index.mjs

from
import { memoize } from "lodash";

to
import { memoize } from "lodash-es";

@rikharink
Copy link

#73 < If i apply this fix to the web variant that I use it also seems to work.

@philipgyllhamn
Copy link

this works for me on nuxt

const tryRemoveBackground = async () => {
  tryRemoveBackgroundStatus.value = true;
  try {
    const imglyRemoveBackground = await import('@imgly/background-removal');
    const config = {
      output: {
        format: "image/png",
        quality: 1
      },
      progress: (key: string, current: number, total: number) => {
        console.log(`Downloading ${key}: ${current} of ${total}`);
      }
    }
    await imglyRemoveBackground.preload(config as any).then(() => {
      console.log("Asset preloading succeeded")
    })
    // imageBackgroundSrc.value = "https://bad.src/not/valid"
    await imglyRemoveBackground.default(imageChosen.value?.originalImageData.base64, config as any).then((blob: Blob) => {
      // result is a blob encoded as PNG.
      // It can be converted to an URL to be used as HTMLImage.src
      //convert blob to base64
      const reader = new FileReader();
      reader.readAsDataURL(blob);
      reader.onloadend = () => {
        const base64data = reader.result;
        imageBackgroundSrc.value = base64data as string;
      }
    })
  } catch (e) {
    console.log(e);
  }
  tryRemoveBackgroundStatus.value = false;
}

this worked on sveltekit for me aswell

@vanling
Copy link

vanling commented May 11, 2024

Could we not change import { memoize } from 'lodash-es'; into import memoize from 'lodash.memoize';?
Adding "lodash.memoize": "^4.1.2" as the package instead of the complete lodash lib?

--

Edit: Changing this locally instantly fixed the Named export 'memoize' not found. in Nuxt3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.