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

Sharp module cannot be loaded if it is a dependency of a dependency #4354

Open
5 tasks done
mrxbox98 opened this issue Mar 27, 2025 · 5 comments
Open
5 tasks done

Sharp module cannot be loaded if it is a dependency of a dependency #4354

mrxbox98 opened this issue Mar 27, 2025 · 5 comments
Labels

Comments

@mrxbox98
Copy link

mrxbox98 commented Mar 27, 2025

Possible bug

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

  • Running npm install sharp completes without error.
  • 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?

  • 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: Windows 11 10.0.22631
    CPU: (16) x64 AMD Ryzen 9 5900HX with Radeon Graphics
    Memory: 13.18 GB / 31.42 GB
  Binaries:
    Node: 20.18.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.22 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD
    bun: 1.2.2 - ~\.bun\bin\bun.EXE

Does this problem relate to file caching?

The default behaviour of libvips is to cache input files, which can lead to EBUSY or EPERM errors on Windows.
Use sharp.cache(false) to switch this feature off.

  • Adding sharp.cache(false) does not fix this problem.

Does this problem relate to images appearing to have been rotated by 90 degrees?

Images that contain EXIF Orientation metadata are not auto-oriented. By default, EXIF metadata is removed.

  • To auto-orient pixel values use the parameter-less rotate() operation.

  • To retain EXIF Orientation use keepExif().

  • Using rotate() or keepExif() does not fix this problem.

What are the steps to reproduce?

Add the package https://github.com/EmergencyBucket/svelte-sharp-image to your sveltekit project. Use as stated in the readme. Running sveltekit on dev mode with vite works fine. Building also works fine. Running a build immediately crashes with the error

          throw new Error(help.join('\n'));
                ^

Error: Could not load the "sharp" module using the win32-x64 runtime
undefined: Could not dynamically require "../src/build/Release/sharp-win32-x64.node". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.
undefined: Could not dynamically require "../src/build/Release/sharp-wasm32.node". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.
undefined: Could not dynamically require "@img/sharp-win32-x64/sharp.node". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.
undefined: Could not dynamically require "@img/sharp-wasm32/sharp.node". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.
Possible solutions:
- Ensure optional dependencies can be installed:
    npm install --include=optional sharp
- Ensure your package manager supports multi-platform installation:
    See https://sharp.pixelplumbing.com/install#cross-platform
- Add platform-specific dependencies:
    npm install --os=win32 --cpu=x64 sharp
- Consult the installation documentation:
    See https://sharp.pixelplumbing.com/install

I tried both putting

build: {
        rollupOptions: {
            external: [
                "sharp"
            ]
        }
    }

in the vite config and the command here: #4001 (comment) but neither worked.

What is the expected behaviour?

No errors.

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

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

@lovell lovell added question and removed triage labels Mar 27, 2025
@lovell
Copy link
Owner

lovell commented Mar 27, 2025

Did you see https://sharp.pixelplumbing.com/install#vite ?

@mrxbox98
Copy link
Author

Did you see https://sharp.pixelplumbing.com/install#vite ?

Yes. Tried that and still encountered the issue.

@lovell
Copy link
Owner

lovell commented Mar 27, 2025

Thanks. Did you try configuring dynamicRequireTargets as mentioned in the error message?

@mrxbox98
Copy link
Author

mrxbox98 commented Apr 1, 2025

Thanks. Did you try configuring dynamicRequireTargets as mentioned in the error message?

I can't currently test this due to vitejs/vite#19727

@mrxbox98
Copy link
Author

mrxbox98 commented Apr 4, 2025

Workaround: Remove sharp from dependency and add it directly to main project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants