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

Error: /lib64/libz.so.1: version `ZLIB_1.2.9' not found (required by /opt/nodejs/node_modules/canvas/build/Release/libpng16.so.16) #1779

Open
JeffersonSchuler opened this issue Apr 5, 2021 · 99 comments

Comments

@JeffersonSchuler
Copy link

JeffersonSchuler commented Apr 5, 2021

When running on AWS Lambda using Node 14 with Canvas v2.7.0 I am seeing the following error:

Error: /lib64/libz.so.1: version `ZLIB_1.2.9' not found (required by /opt/nodejs/node_modules/canvas/build/Release/libpng16.so.16)

I am successfully running on Node 14 using Canvas v2.6.1, but wanted to upgrade. This library is a dependency of
ChartjsNodeCanvas which is ultimately what I am calling.

Thanks.

@adamsoffer
Copy link

Getting the same error running on Vercel Node 14 :/

@jaggr2
Copy link

jaggr2 commented Apr 14, 2021

I made some tests:

  • Canvas v2.7.0 is working on AWS Lambda Node 12, but not 14
  • It fails even when canvas is the only and first dependency in the lambda handler script, so the shared zlib dependency is preloaded by something else.
  • NodeJS 12 and 14 on AWS Lambda are using zlib version 1.2.11 according to console.log(process.version) . But I guess zlib is compiled directly into NodeJS and is not using the shared library, so this does not matter
  • I tried to inject the correct libz.so.1 via a lambda layer in /lib64 path, but without any success yet. The LD_LIBRARY_PATH environment variable on my lambda has following value "LD_LIBRARY_PATH": "/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib", While listing the /lib64 directory I noticed that there is a libz.so.1.2.7 as well, but I don't know from where this one comes from.

@JeffersonSchuler
Copy link
Author

JeffersonSchuler commented Apr 14, 2021

I had also tried to inject the correct zlib version with no luck. Will circle back when I have some time. At this moment, Canvas v2.6.1 is working fine for me.

@charoitel
Copy link

charoitel commented May 25, 2021

I built Canvas v2.8.0 from source on Amazon Linux 2, and now published the packaged layer on AWS Serverless Application Repository (https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:990551184979:applications~lambda-layer-canvas-nodejs). Hope this layer could help.

@alvarlagerlof
Copy link

I've got the same issue running on Vercel

@mraerino
Copy link

mraerino commented Jun 16, 2021

this seems to happen based on LD_LIBRARY_PATH set. in AWS lambda it is /lib64:/usr/lib64:$LAMBDA_RUNTIME_DIR:$LAMBDA_RUNTIME_DIR/lib:$LAMBDA_TASK_ROOT:$LAMBDA_TASK_ROOT/lib:/opt/lib

there is a libz.so.1 in /lib64 so although canvas includes a libz.so.1 the one from the OS gets loaded first.

two possible solutions:

  • add /var/task/node_modules/canvas/build/Release to the beginning of LD_LIBRARY_PATH
  • set LD_PRELOAD=/var/task/node_modules/canvas/build/Release/libz.so.1

to properly fix this in canvas i think the libs linking to libz should ensure they can differentiate, e.g. by linking against .1.2.9 directly instead of just .1 or by using static linking.

@ludovicraymond
Copy link

this seems to happen based on LD_LIBRARY_PATH set. in AWS lambda it is /lib64:/usr/lib64:$LAMBDA_RUNTIME_DIR:$LAMBDA_RUNTIME_DIR/lib:$LAMBDA_TASK_ROOT:$LAMBDA_TASK_ROOT/lib:/opt/lib

there is a libz.so.1 in /lib64 so although canvas includes a libz.so.1 the one from the OS gets loaded first.

two possible solutions:

  • add /var/task/node_modules/canvas/build/Release to the beginning of LD_LIBRARY_PATH
  • set LD_PRELOAD=/var/task/node_modules/canvas/build/Release/libz.so.1

to properly fix this in canvas i think the libs linking to libz should ensure they can differentiate, e.g. by linking against .1.2.9 directly instead of just .1 or by using static linking.

set LD_PRELOAD works for me !

@alvarlagerlof
Copy link

set LD_PRELOAD works for me !

How do I do this in Vercel?

@kagawagao
Copy link

set LD_PRELOAD works for me !

How do I do this in Vercel?

as @JeffersonSchuler said, use [email protected], it worked for me

@mraerino
Copy link

as @JeffersonSchuler said, use [email protected], it worked for me

didn't work for me on AWS lambda with nodejs14.x

@JeffersonSchuler
Copy link
Author

as @JeffersonSchuler said, use [email protected], it worked for me

didn't work for me on AWS lambda with nodejs14.x

I am using it in a Layer, not sure if it makes a difference.

@ludovicraymond
Copy link

ludovicraymond commented Jun 17, 2021

@mraerino

The solution that works on Lambda with node 14.x and [email protected] :

  • Create an EC2 compatible with Lambda (I have used the AMI amzn-ami-hvm-2018.03.0.20181129-x86_64-gp2)
  • Install node 14, install your packages with npm for example, download your packages (node_modules) with tar & scp for example and use them for your lambda function deployment.
  • Add a layer on your lambda function (I used the lib64 version) : https://github.com/jwerre/node-canvas-lambda
  • Define an environment variable : LD_PRELOAD=/var/task/node_modules/canvas/build/Release/libz.so.1

That's it !

@mraerino
Copy link

When deploying through Netlify I was able to just install regularly through npm and didn't need a special layer, but customized the LD_LIBRARY_PATH

@JeffersonSchuler
Copy link
Author

I built Canvas v2.8.0 from source on Amazon Linux 2, and now published the packaged layer on AWS Serverless Application Repository (https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:990551184979:applications~lambda-layer-canvas-nodejs). Hope this layer could help.

Thanks for doing the layer, this worked for me. I was already using a layer for some of the bigger packages like canvas and did not want to package them into the main function deployment. I tried the two environment variable solutions suggested here with no luck.

pouretrebelle added a commit to pouretrebelle/jigsaws that referenced this issue Jun 27, 2021
I really want to ship these layout changes but continue to have issues
with zlib (see Automattic/node-canvas#1779)
which I won't have the capacity to investigate or solve for at least a
few weeks. Hiding these features and merging until I can look into it
fully.
pouretrebelle added a commit to pouretrebelle/jigsaws that referenced this issue Jun 27, 2021
I really want to ship these layout changes but continue to have issues
with zlib (see Automattic/node-canvas#1779)
which I won't have the capacity to investigate or solve for at least a
few weeks. Hiding these features and merging until I can look into it
fully.
pouretrebelle added a commit to pouretrebelle/jigsaws that referenced this issue Jun 27, 2021
I really want to ship these layout changes but continue to have issues
with zlib (see Automattic/node-canvas#1779)
which I won't have the capacity to investigate or solve for at least a
few weeks. Hiding these features and merging until I can look into it
fully.
pouretrebelle added a commit to pouretrebelle/jigsaws that referenced this issue Jun 27, 2021
I really want to ship these layout changes but continue to have issues
with zlib (see Automattic/node-canvas#1779)
which I won't have the capacity to investigate or solve for at least a
few weeks. Hiding these features and merging until I can look into it
fully.
@jacobdubail
Copy link

set LD_PRELOAD works for me !

How do I do this in Vercel?

Did you figure this out? I'm stuck.

@andrewbarnesweb
Copy link

How do I do this in Vercel?

Not sure if it helps, if you need the latest features, but as above. Sorted it for me on Vercel by using [email protected].

@jacobdubail
Copy link

set LD_PRELOAD works for me !

How do I do this in Vercel?

Did you figure this out? I'm stuck.

The issue was fabric. It includes an outdated version of jsdom, which references canvas 2.5.0. I'm using fabric-pure-browser now, which removes the canvas dependency.

@nikmish1
Copy link

I've got the same issue running on Vercel

Hi, were you able to resolve it. I am trying to run worpress starter with some custom pages. But getting the above error though everything runs well locally.

@nikmish1
Copy link

this seems to happen based on LD_LIBRARY_PATH set. in AWS lambda it is /lib64:/usr/lib64:$LAMBDA_RUNTIME_DIR:$LAMBDA_RUNTIME_DIR/lib:$LAMBDA_TASK_ROOT:$LAMBDA_TASK_ROOT/lib:/opt/lib

there is a libz.so.1 in /lib64 so although canvas includes a libz.so.1 the one from the OS gets loaded first.

two possible solutions:

  • add /var/task/node_modules/canvas/build/Release to the beginning of LD_LIBRARY_PATH
  • set LD_PRELOAD=/var/task/node_modules/canvas/build/Release/libz.so.1

to properly fix this in canvas i think the libs linking to libz should ensure they can differentiate, e.g. by linking against .1.2.9 directly instead of just .1 or by using static linking.

adding LD_LIBRARY_PATH=/var/task/node_modules/canvas/build/Release worked for me. Thanks

@paulschneider
Copy link

How do I do this in Vercel?

Not sure if it helps, if you need the latest features, but as above. Sorted it for me on Vercel by using [email protected].

Not related to Vercel but using Canvas in Node14 we experienced this same problem on our build server; downgrading to 2.61 resolved the issue for us.

@SimplyLinn
Copy link

For people wanting this on Vercel. Due to Iterative Static Regeneration and the dynamic loading you can get with Vercel in general, you need to set LD_LIBRARY_PATH properly for all instances of the Vercel processes that are spawned, not just the initial buildscript.

// For building on vercel: https://github.com/Automattic/node-canvas/issues/1779
if (
  process.env.LD_LIBRARY_PATH == null ||
  !process.env.LD_LIBRARY_PATH.includes(
    `${process.env.PWD}/node_modules/canvas/build/Release:`,
  )
) {
  process.env.LD_LIBRARY_PATH = `${
    process.env.PWD
  }/node_modules/canvas/build/Release:${process.env.LD_LIBRARY_PATH || ''}`;
}

Adding this to the top of next.config.js made Vercel build the project for me, and it will hopefully carry over to any processes spawned in the future that also has to load next.config.js.

@arrokh
Copy link

arrokh commented Aug 10, 2021

For people wanting this on Vercel. Due to Iterative Static Regeneration and the dynamic loading you can get with Vercel in general, you need to set LD_LIBRARY_PATH properly for all instances of the Vercel processes that are spawned, not just the initial buildscript.

// For building on vercel: https://github.com/Automattic/node-canvas/issues/1779
if (
  process.env.LD_LIBRARY_PATH == null ||
  !process.env.LD_LIBRARY_PATH.includes(
    `${process.env.PWD}/node_modules/canvas/build/Release:`,
  )
) {
  process.env.LD_LIBRARY_PATH = `${
    process.env.PWD
  }/node_modules/canvas/build/Release:${process.env.LD_LIBRARY_PATH || ''}`;
}

Adding this to the top of next.config.js made Vercel build the project for me, and it will hopefully carry over to any processes spawned in the future that also has to load next.config.js.

I can't believe, it perfectly works. Thank you so much @SimplyLinn

@BrunoQuaresma
Copy link

BrunoQuaresma commented Aug 17, 2021

@SimplyLinn and @arrokh what "now-build" script are you using? The proposed solution for Vercel didn't work for me 😥

I'm still seeing the following error:

ERROR	Error: /lib64/libz.so.1: version `ZLIB_1.2.9' not found (required by /var/task/node_modules/canvas/build/Release/libpng16.so.16)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1144:18)

and the "now-build" script that I'm using is:

{
  ...
  "now-build": "yum install libuuid-devel libmount-devel && cp /lib64/{libuuid,libmount,libblkid}.so.1 node_modules/canvas/build/Release/ && npm run build"
}

I'm using "canvas": "^2.7.0" and NodeJS 14.

@SimplyLinn
Copy link

SimplyLinn commented Aug 17, 2021

@SimplyLinn and @arrokh what "now-build" script are you using? The proposed solution for Vercel didn't work for me 😥

I'm still seeing the following error:

ERROR	Error: /lib64/libz.so.1: version `ZLIB_1.2.9' not found (required by /var/task/node_modules/canvas/build/Release/libpng16.so.16)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1144:18)

and the "now-build" script that I'm using is:

{
  ...
  "now-build": "yum install libuuid-devel libmount-devel && cp /lib64/{libuuid,libmount,libblkid}.so.1 node_modules/canvas/build/Release/ && npm run build"
}

I'm using "canvas": "^2.7.0" and NodeJS 14.

Using canvas@^2.8.0.
package.json

{
    "build": "yarn bootstrap && next build",
    "bootstrap": "run-p -l --silent --aggregate-output bootstrap:*",
    "bootstrap:robots-txt": "NODE_ENV=production node bootstrap-robots.js",
}

The bootstrap process is just doing some robots.txt schenanigans based on stuff and places it in the public folder and has little to do with the canvas library

Install command override configured in vercel:
yarn install --production="false" && rm -f node_modules/canvas/build/Release/librsvg-2.so.2 (We do not need svg support for our uses, and librsvg was over 100mb and made our bundle approach the size limit of vercel)

Since you were using canvas@^2.7.0, I'd suggest you try latest version of canvas maybe? Otherwise I'm out of advice.

@abdulmomin956
Copy link

abdulmomin956 commented Oct 1, 2023

Where is the Netlify solution? canvas : ^2.11.2, node: v16.20.2 LTS (in Netlify)
Edit:
After 3 days of failure, I realized that Canvas version 2.11.0 is mandatory for Netlify and of course, we have to add LD_LIBRARY_PATH environment variable in Netlify. The other two options of https://github.com/okwme/serverless-canvas/ are ignorable. Thanks to @okwme

@coindegen
Copy link

Where is the Netlify solution? canvas : ^2.11.2, node: v16.20.2 LTS (in Netlify) Edit: After 3 days of failure, I realized that Canvas version 2.11.0 is mandatory for Netlify and of course, we have to add LD_LIBRARY_PATH environment variable in Netlify. The other two options of https://github.com/okwme/serverless-canvas/ are ignorable. Thanks to @okwme

this answer worked for me too, thanks for linking!

@iget-master
Copy link

iget-master commented Oct 18, 2023

I'm having issue with Error: libpixman-1.so.0: cannot open shared object file: No such file or directory even with the given solution here. Someone found problems with it?

@nrathi
Copy link

nrathi commented Oct 22, 2023

@thomasvalera I am using [email protected], at first It was failing because vercel is using node 18 as default. When I set the LD_LIBRARY_PATH I started getting GLIBC_2.28 but the downgrade fixed it.

Downgrade to what?

@iget-master
Copy link

iget-master commented Oct 22, 2023 via email

@abdulmomin956
Copy link

@thomasvalera I am using [email protected], at first It was failing because vercel is using node 18 as default. When I set the LD_LIBRARY_PATH I started getting GLIBC_2.28 but the downgrade fixed it.

Downgrade to what?

Downgrade to Node 16

@matissoz
Copy link

matissoz commented Oct 27, 2023

@abdullah-live I successfully replaced canvas with @napi-rs/canvas and it works on Vercel with Node 18. They are very similar with only some minor differences. You should be able to easily swap between the two packages.

Here is a great example on how to use it.

@ernestd
Copy link

ernestd commented Nov 21, 2023

most of the solutions above require to downgrade to node 16 so we basically have 3 months left until we find a solution for v18

@zhyd1997
Copy link

https://github.com/orgs/vercel/discussions/3061#discussioncomment-7599429

@johncalvinroberts
Copy link

If anyone is coming due to fabric's depency on canvas failing to bundle for next.js with Node.js 18 and higher, the following is a solution that worked for me.

Add this to package.json:

"canvas": "https://registry.yarnpkg.com/@favware/skip-dependency/-/skip-dependency-1.2.1.tgz"

This will simply stub the canvas dependency with a noop package. So far no issues.

@fausanchez
Copy link

@johncalvinroberts Did you make fabric work with next js 14 app route? Can you please share an example of your configuration? I've been trying almost every solution out there, but nothing seems to work

@omatheusant
Copy link

please, i'm having the same problem with fabricjs and nextjs 14 when I try to deploy on vercel. If someone can do it work, i'd love some help.

@zhyd1997
Copy link

Upgrade Nodejs version from v16 to v20 works for me!

@omatheusant
Copy link

you are a NextJS 14? i'm stuck with this error with hours, if you could help me

@zhyd1997
Copy link

you are a NextJS 14? i'm stuck with this error with hours, if you could help me

You can try upgrade nodejs version to 20.

@Peixer
Copy link

Peixer commented Dec 19, 2023

Yeah, using NodeJS version 20, worked for me too!!

@itsanishjain
Copy link

fabric-pure-browser

Using this?
https://www.npmjs.com/package/fabric-pure-browser

@itsanishjain
Copy link

If anyone is coming due to fabric's depency on canvas failing to bundle for next.js with Node.js 18 and higher, the following is a solution that worked for me.

Add this to package.json:

"canvas": "https://registry.yarnpkg.com/@favware/skip-dependency/-/skip-dependency-1.2.1.tgz"

This will simply stub the canvas dependency with a noop package. So far no issues.

If anyone is coming due to fabric's depency on canvas failing to bundle for next.js with Node.js 18 and higher, the following is a solution that worked for me.

Add this to package.json:

"canvas": "https://registry.yarnpkg.com/@favware/skip-dependency/-/skip-dependency-1.2.1.tgz"

This will simply stub the canvas dependency with a noop package. So far no issues.

Ok this worked now

 "dependencies": {
    "fabric": "^5.3.0",
    "next": "14.0.4",
    "react": "^18",
    "react-dom": "^18",
    "canvas": "https://registry.yarnpkg.com/@favware/skip-dependency/-/skip-dependency-1.2.1.tgz"
  },

@danesto
Copy link

danesto commented Jan 26, 2024

If anyone is coming due to fabric's depency on canvas failing to bundle for next.js with Node.js 18 and higher, the following is a solution that worked for me.
Add this to package.json: "https://registry.yarnpkg.com/@favware/skip-dependency/-/skip-dependency-1.2.1.tgz"

This one worked for me as well using:

- node 18x on Vercel 
- next "14.0.4"
- react-konva "^18.2.10"

...after day of struggling to find the solution. It is weird since react-konva is proposing to update the nextConfig file in order to avoid canvas errors, importing it dynamically as a client component, etc. But if you use it on client side you probably shouldn't bother with installing packages to vercel using yum and similar config, it would just be enough to find some way to ignore canvas build error, right? But for some reason it would still throw the error for canvas like if you're trying to run with SSR.

Hard coding this "canvas": "https://registry.yarnpkg.com/@favware/skip-dependency/-/skip-dependency-1.2.1.tgz" without any additional config solved the issue for me.

But nevertheless it seems like non of the official docs are actually describing the right solution for the node-canvas issue and how to solve it the right way, It is really strange, since a lot of libs are built on top of it. 🤔 For now this workaround seem to work.

@liowalex
Copy link

I got it working with vercel after much trial and error using:

Next: latest
Canvas: latest
Node: 20.x <-- this is crucial, builds take a little longer, but it works!

@iyhub
Copy link

iyhub commented Mar 18, 2024

I've got the same issue running on Vercel我在 Vercel 上运行时遇到同样的问题

Hey bro,Have you solved it?

@danesto
Copy link

danesto commented Mar 18, 2024

s": "https://registry.yarnpkg.com/@favware/skip-dependency/-/skip-dependency-1.2.1.tgz"

This one worked for me as well using:

- node 18x on Vercel 
- next "14.0.4"
- react-konva "^18.2.10"

...after day of struggling to find the solution. It is weird since react-konva is proposing to update the nextConfig file in order to avoid canvas errors, importing it dynamically as a client component, etc. But if you use it on client side you probably shouldn't bother with installing packages to vercel using yum and similar config, it would just be enough to find some way to ignore canvas build error, right? But for some reason it would still throw the error for canvas like if you're trying to run with SSR.

Hard coding this "canvas": "https://registry.yarnpkg.com/@favware/skip-dependency/-/skip-dependency-1.2.1.tgz" without any additional config solved the issue for me.

But nevertheless it seems like non of the official docs are actually describing the right solution for the node-canvas issue and how to solve it the right way, It is really strange, since a lot of libs are built on top of it. 🤔 For now this workaround seem to work.

@iyhub Have you tried this?

@mino9421
Copy link

after lots of trial and error and misleading lazy solutions that requires downgrading. I can confirm that Node 20 on vercel which is beta work with me unlike 18 and 16 which is almost expired.
so the following specs on a working deployment are:

node: 20
canvas: 2.8.0
and I included the following to the top of the next config file
if ( process.env.LD_LIBRARY_PATH == null || !process.env.LD_LIBRARY_PATH.includes( ${process.env.PWD}/node_modules/canvas/build/Release:) ) { process.env.LD_LIBRARY_PATH =${
process.env.PWD
}/node_modules/canvas/build/Release:${process.env.LD_LIBRARY_PATH || ""}; }
Hope this was helpful. If anyone faces error after duplicating same configs. comment and ill see what can I do. I am jobless just like every other developer and free to help

@alwalxed
Copy link

https://github.com/orgs/vercel/discussions/3061#discussioncomment-7599429

Thank you very much! This worked for me on:

{
  "next": "14.1.4"
  "fabric": "^5.3.0"
}

In brief, if you're looking for a quick summary: navigate to your project settings on Vercel, locate the General section, switch the Node.js Version from 18.x to 20.x, rebuild your project, and that's all it takes!

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

No branches or pull requests