sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Nuxt v3 with Nitro worker threads - Module did not self-register

Open juno-w opened this issue 3 years ago • 11 comments

Possible bug

Is this a possible bug in a feature of sharp, unrelated to installation?

  • [x] Running npm install sharp completes without error.
  • [x] Running node -e "require('sharp')" completes without error.

If you cannot confirm both of these, please open an installation issue instead.

Are you using the latest version of sharp?

  • [x] I am using the latest version of sharp as reported by npm view sharp dist-tags.latest.

If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.

If you are using another package which depends on a version of sharp that is not the latest, please open an issue against that package instead.

What is the output of running npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp?

System:
    OS: macOS 12.3.1
    CPU: (10) x64 Apple M1 Pro
    Memory: 21.15 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.19.0 - ~/.nvm/versions/node/v14.19.0/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 8.14.0 - ~/.nvm/versions/node/v14.19.0/bin/npm

What are the steps to reproduce?

Install sharp package from Nuxt 3 RC5 and import * as sharp from "sharp"; I have even tried to install using --platform and --arch but still not working.

What is the expected behaviour?

not available

Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem

not available

Please provide sample image(s) that help explain this problem

image

juno-w avatar Jul 14 '22 11:07 juno-w

Please see https://sharp.pixelplumbing.com/install#worker-threads

lovell avatar Jul 14 '22 11:07 lovell

Hi @lovell thanks for the prompt response.

Btw, how do I tell my Nuxt 3 on this Worker Thread ?

juno-w avatar Jul 15 '22 03:07 juno-w

Perhaps you could create a minimal repo that allows someone else to reproduce.

lovell avatar Jul 15 '22 08:07 lovell

Found this thread https://github.com/lovell/sharp/issues/2981

It seems not working on Nuxt 3?

juno-w avatar Jul 15 '22 14:07 juno-w

#2981 relates to the creation of an ESM wrapper for sharp; mention of Nuxt is coincidental.

lovell avatar Jul 15 '22 15:07 lovell

#2981 relates to the creation of an ESM wrapper for sharp; mention of Nuxt is coincidental.

Sorry, I'm still don't get it 🥹 Is it really Nuxt 3 can't use Sharp yet?

juno-w avatar Jul 17 '22 02:07 juno-w

Please create a minimal repo that allows someone else to reproduce, then we can start to answer your questions.

lovell avatar Jul 17 '22 09:07 lovell

Here's the link https://stackblitz.com/edit/nuxt-starter-hw78au?file=server%2Fapi%2Fsharp.ts

run npm run dev and you will encounter the error.

juno-w avatar Jul 18 '22 02:07 juno-w

Thank you. Although Stackblitz doesn't work, I was able to download the project and run it locally to reproduce.

Remembering that https://sharp.pixelplumbing.com/install#worker-threads says:

the main thread must call require('sharp') before worker threads are created.

I can see that Nuxt uses Nitro for its local dev server, which uses worker threads.

https://github.com/unjs/nitro/blob/main/src/dev/server.ts

The Nitro docs state that it provides hooks that allow you to inject custom lifecycle logic.

https://nitro.unjs.io/config/#hooks

Putting all this together, the following appears to work:

export default defineNuxtConfig({
    nitro: {
        hooks: {
            'dev:reload': () => require('sharp')
        }
    }
})

lovell avatar Jul 18 '22 08:07 lovell

As an aside, the Cannot call a namespace ('sharp') warnings can be silenced with the following change:

- import * as sharp from 'sharp';
+ import sharp from 'sharp';

lovell avatar Jul 18 '22 08:07 lovell

Thanks Lovell, I was able to run following your method. Unfortunately sharp doesn't accept ArrayBuffer. Will dig further on this. image

juno-w avatar Jul 18 '22 10:07 juno-w

@juno-w Were you able to make progress with this?

lovell avatar Aug 21 '22 19:08 lovell

@juno-w Were you able to make progress with this?

Hi @lovell , the buffer() is not working though. I will close this issue and reopen it once I get more details on this.

juno-w avatar Aug 22 '22 02:08 juno-w