lovell / sharp

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.
https://sharp.pixelplumbing.com
Apache License 2.0
28.68k stars 1.28k forks source link

Support for bun runtime #3511

Closed asilvas closed 8 months ago

asilvas commented 1 year ago

Feature request

What are you trying to achieve?

Bun seems like a good fit for sharp/vips with the focus on performance. The reduction in overhead from bindings could be a win with Bun. Currently no good image processing package for Bun, so this would given even more reason to promote sharp.

When you searched for similar feature requests, what did you find that might be related?

Nothing

What would you expect the API to look like?

n/a

What alternatives have you considered?

Currently use sharp extensively with Node.

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

n/a

[8.68ms] "node_modules.bun" - 15 modules, 8 packages
29 |     if (loadedModule) {
30 |       const [, loadedPackage] = loadedModule.match(/node_modules[\\/]([^\\/]+)[\\/]/);
31 |       help.push(`- Ensure the version of sharp aligns with the ${loadedPackage} package: "npm ls sharp"`);
32 |     }
33 |   }
34 |   throw new Error(help.join('\n'));
            ^
error:
Something went wrong installing the "sharp" module

Cannot find module "../build/Release/sharp-linux-x64.node" from "./node_modules/sharp/lib/sharp.js"

Possible solutions:
- Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
- Install for the current linux-x64 runtime: "npm install --platform=linux --arch=x64 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install
lovell commented 1 year ago

This will be dependent on https://github.com/oven-sh/bun/issues/158 and https://github.com/oven-sh/bun/issues/606

asilvas commented 1 year ago

This will be dependent on oven-sh/bun#158 and oven-sh/bun#606

Great thanks. I suspect those issues will be unblocked in the next couple months at the rate they're going. I'll try to remember to update this thread once ready.

birkskyum commented 1 year ago

@lovell , would you be able to narrow down what parts of https://github.com/oven-sh/bun/issues/158 are lacking in order for sharp to function?

lovell commented 1 year ago

@birkskyum It looks like almost all of Node-API has been implemented in Bun during the last 6 months - perhaps someone can test it again?

birkskyum commented 1 year ago

Okay, interesting that it could be resolved already. The other issue mentioned breaks on installation (https://github.com/oven-sh/bun/issues/606), so let's assess the situation when that is fixed.

infrahead commented 1 year ago

Yes, I was pleasantly surprised to see sharp working on Bun v0.6.14! This was based on some very simple scripts I was using to keep tabs on this progress, not a test of the full API. To @birkskyum point, this is possible as I'm using my own install script (with prebuilt binaries) to avoid all the npm install overhead and lifecycle script permissions.

birkskyum commented 1 year ago

Landing WebAssembly build #3522 would be a great step to resolve this too, as it would provide a very portable fallback with reduced performance.

JawadAhbab commented 12 months ago

Can anyone give me a working example please?

paperdave commented 12 months ago

Once Bun 0.8.0 releases, Sharp will work (napi features sharp depends on is more stable/fixed a bad crash), though the NPM lifecycle script not running is still an issue, see https://github.com/oven-sh/bun/issues/606

xhyrom commented 12 months ago

For now, you can manually run post install script

birkskyum commented 12 months ago

@xHyroM , how do i run postinstall with bun?

xhyrom commented 12 months ago

@xHyroM , how do i run postinstall with bun?

cd node_modules/sharp
bun install

then wait until install and try to run your code again, it should work :D

birkskyum commented 12 months ago

@xHyroM , it works directly on my machine, which is awesome, but when doing this within a Dockerfile I get:

error: script not found "install/libvips"
error: script not found "install/can-compile"

My entire dockerfile is:

FROM oven/bun

WORKDIR /usr/repo

COPY . .

RUN bun install
RUN cd ./node_modules/sharp && bun install

EXPOSE 3001

CMD [ "bun", "--bun", "run", "./src/app.ts" ]
birkskyum commented 12 months ago

Seems like the script can be found by making the paths in sharp's package.json relative, so they are i.e. ./install/libvips.js install of install/libvips.js - unfortunately that still yields:

sharp: Installation error: detectLibc.versionSync is not a function. (In 'detectLibc.versionSync()', 'detectLibc.versionSync' is undefined)
sharp: Please see https://sharp.pixelplumbing.com/install for required dependencies
xhyrom commented 12 months ago

Not sure what to do, with npm you could do npm rebuild --arch=x64 --platform=linux sharp but you want bun

birkskyum commented 12 months ago

I realized that the bun runtime works with sharp, but that bun install doesn't, so an easy solution was to just install with npm and run with bun after.

birkskyum commented 11 months ago

Normal Sharp install in Bun will likely be resolved by:

Then sharp just has to be added in package.json as trustedDependencies: ["sharp"]

xhyrom commented 11 months ago

Normal Sharp install in Bun will likely be resolved by:

Then sharp just has to be added in package.json as trustedDependencies: ["sharp"]

Can you try to put sharp into trustedDependencies and try bun install again? It should run the install script. Just want to know if it resolve the libc versionSync error

birkskyum commented 11 months ago

With latest canary, and sharp in trustedDependencies, I get this, and i think it's because it can't find the arm64 bundle:

➜ bun run start
$ bun run ./src/app.ts
[0.01ms] ".env"
error: script "start" exited with code 11 (SIGSEGV)
infrahead commented 11 months ago

Simply using npm install in case of sharp seems to be the answer for now. I'd also suggest that the real solution will be #3750 and not Bun's trustedDependencies convenience feature, as some will never allow install scripts.

birkskyum commented 11 months ago

I made a separate ticket for supporting bun install. This ticket about bun runtime appear to be resolved.

Jarred-Sumner commented 11 months ago

With latest canary, and sharp in trustedDependencies, I get this, and i think it's because it can't find the arm64 bundle:

➜ bun run start
$ bun run ./src/app.ts
[0.01ms] ".env"
error: script "start" exited with code 11 (SIGSEGV)

It is unlikely that bun not finding the .node file would cause a segfault. It sounds like we still have a crash to fix here

birkskyum commented 11 months ago

Result of using the debug build:

loader [parsing] /Users/admin/repos/backend/node_modules/stripe/esm/autoPagination.js
Loader [resolve] ./utils.js
loader [parsing] /Users/admin/repos/backend/node_modules/stripe/esm/multipart.js
Loader [resolve] ./utils.js
32 |     if (loadedModule) {
33 |       const [, loadedPackage] = loadedModule.match(/node_modules[\\/]([^\\/]+)[\\/]/);
34 |       help.push(`- Ensure the version of sharp aligns with the ${loadedPackage} package: "npm ls sharp"`);
35 |     }
36 |   }
37 |   throw new Error(help.join('\n'));
            ^
error: 
Something went wrong installing the "sharp" module

Cannot find module "../build/Release/sharp-darwin-arm64v8.node" from "/Users/admin/repos/backend/node_modules/sharp/lib/sharp.js"

Possible solutions:
- Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
- Install for the current darwin-arm64v8 runtime: "npm install --platform=darwin --arch=arm64v8 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install
      at /Users/admin/repos/backend/node_modules/sharp/lib/sharp.js:37:8
      at globalThis (/Users/admin/repos/backend/node_modules/sharp/lib/sharp.js:37:33)
      at require (:1:20)
      at /Users/admin/repos/backend/node_modules/sharp/lib/constructor.js:11:0
      at globalThis (/Users/admin/repos/backend/node_modules/sharp/lib/constructor.js:439:17)
      at require (:1:20)
      at /Users/admin/repos/backend/node_modules/sharp/lib/index.js:6:6
      at globalThis (/Users/admin/repos/backend/node_modules/sharp/lib/index.js:16:17)
      at require (:1:20)
      at /Users/admin/repos/backend/node_modules/join-images/lib/main.js:7:24
      at globalThis (/Users/admin/repos/backend/node_modules/join-images/lib/main.js:89:18)
error: script "start" exited with code 1 (SIGHUP)
hendrikboeck commented 10 months ago

I had the same problem, it seems like it is related to bun install not running a postinstall hook. You can try the workaround below, but you will not be able to build or compile your program, as these options run a separate bun install command on startup, which doesn't use your local node_modules and ignores trustedDependencies for some reason.

I am no expert in Bun, but I think it's caused by the different install method (probably something with linking instead of clean installing) of Bun. But I am too stupid myself to understand how exactly it differs and how to possibly fix it.

Current Workaround:

Add sharp under trustedDependencies in package.json

{
  ...
  "trustedDependencies": ["sharp"]
}

and then run

rm -rf node_modules
rm -rf bun.lockb
bun install

This issue seems to be related to https://github.com/oven-sh/bun/issues/4959

More information on trustedDependencies under https://bun.sh/docs/cli/install#trusted-dependencies

qorbani commented 10 months ago

@hendrikboeck in your message and code snippet you misspelled trustedDependencies 😄

lovell commented 10 months ago

Please take a look at https://github.com/lovell/sharp/issues/3750#issuecomment-1751172728 if you'd like to test an early pre-release of the forthcoming sharp v0.33.0 that should "just work" with Bun without setting trustedDependencies.

MariuzM commented 9 months ago

Sharp sharp@0.33.0-alpha.11 works locally with Bun but when i deploy to railway i get this error

image

Only diff is that i'm using docker on railway

FROM oven/bun:1.0.12-alpine

WORKDIR /usr/src/app
COPY . .
RUN bun install
RUN bun run build

ENV PORT 3001

EXPOSE 3000

CMD bun run start
lovell commented 9 months ago

@MariuzM https://github.com/lovell/sharp/pull/3848 will fix the wrong error being shown, but there's still an underlying problem. It looks like you're using some kind of bundler, so please ensure sharp is marked as "external" - see https://sharp.pixelplumbing.com/install#bundlers

MariuzM commented 9 months ago

I don't think i use anything else apart what Bun already gives, my build command is bun build ./src/app.ts --outdir ./build --target bun

i have tried bun build ./src/app.ts --outdir ./build --target bun --external sharp

but still same issue

lovell commented 9 months ago

@MariuzM This looks like something specific to the bun build command itself. Perhaps you could create a minimal, standalone repo that allows someone else to reproduce.

MariuzM commented 9 months ago

@lovell https://github.com/MariuzM/bun-sharp and i have tested on Railway still same issue

MariuzM commented 9 months ago

Ive just tested locally with Docker and issue is there, but when NOT using docker i don't have this issue, so not sure what difference does docker make, my docker config looks simple enough

# https://hub.docker.com/r/oven/bun/tags?page=1&name=alpine
FROM oven/bun:1.0.12-alpine

WORKDIR /usr/src/app
COPY . .
RUN bun install
RUN bun run build

ENV PORT 3001

EXPOSE 3000

CMD bun run start
image
hendrikboeck commented 9 months ago

@MariuzM I fixed your problem. I think you forgot to add semver to your package.json and forgot to install vips-dev in Alpine, as you declared it as external.

my Dockerfile:

# https://hub.docker.com/r/oven/bun/tags?page=1&name=alpine
FROM oven/bun:1.0.12-alpine

RUN apk add vips-dev

WORKDIR /usr/src/app
COPY . .
RUN bun install
RUN bun run build

EXPOSE 3000
CMD bun run start

my package.json:

{
  "name": "bun-sharp",
  "version": "1.0.0",
  "scripts": {
    "d": "bun run --watch src/app.ts",
    "b": "bun build ./src/app.ts --outdir ./build --target bun --external sharp",
    "dev": "bun run d",
    "build": "bun run b",
    "start": "bun run ./build/app.js"
  },
  "dependencies": {
    "elysia": "^0.7.28",
    "semver": "^7.5.4",
    "sharp": "0.33.0-alpha.11"
  },
  "devDependencies": {
    "@types/node": "^20.9.1",
    "@types/uuid": "^9.0.7",
    "@typescript-eslint/eslint-plugin": "^6.11.0",
    "@typescript-eslint/parser": "^6.11.0",
    "bun-types": "^1.0.12",
    "eslint": "8.54.0",
    "eslint-plugin-import": "^2.29.0",
    "prettier": "^3.1.0"
  },
  "module": "src/app.js"
}

my .dockerignore:

/node_modules
/build
MariuzM commented 9 months ago

@hendrikboeck thank you this worked. Its not that i forgot i just did not know 😁

lovell commented 9 months ago

@MariuzM Whilst it might appear to work, there is no need to add the vips-dev package. In addition, the fact that you also had to add a dependency on semver despite your code not depending upon it does rather suggest that bun build is still not working as advertised.

lovell commented 9 months ago

@MariuzM Your container uses Alpine Linux but Bun does not officially support musl-based Linux - https://github.com/oven-sh/bun/issues/918

In addition, Bun polyfills the detect-libc dependency and returns a hard-coded glibc on all Linux platforms - see https://github.com/oven-sh/bun/blob/main/src/js/thirdparty/detect-libc.linux.js

MariuzM commented 9 months ago

@lovell thank you for taking time to explain in more detail, dint realized Alpine had missing packages.

lovell commented 8 months ago

v0.33.0 is now available and tested working with the Bun JavaScript runtime on both Linux and macOS.

https://github.com/lovell/sharp/actions/runs/7032565186

MariuzM commented 8 months ago

THANK YOUUUUU

7heMech commented 6 months ago

Nope, it doesn't work. I'm getting this error in a replit's nix environment, just by trying to import it.

Could not load the "sharp" module using the linux-x64 runtime undefined: libstdc++.so.6: cannot open shared object file: No such file or directory

Repl link

lovell commented 6 months ago

@7heMech It looks like you're using 0.33.0-alpha.6, which is an unsupported pre-release version. Please also ensure you're using the latest Bun as the .replit file suggests an older version.