This error occurs when installing node-canvas without the required system dependencies. Install Cairo and related graphics libraries for your Linux distribution to fix it.
The node-canvas package is a Cairo-backed Canvas implementation for Node.js. It requires several native graphics libraries to be installed on your system before npm can build it. pkg-config is a helper tool that helps the build process find installed libraries. When it's missing, the compilation can't locate the required dependencies. This error commonly occurs on minimal server installations, Docker containers, or CI environments that don't include development libraries by default.
Install all required packages:
sudo apt-get update
sudo apt-get install -y \
build-essential \
libcairo2-dev \
libpango1.0-dev \
libjpeg-dev \
libgif-dev \
librsvg2-dev \
pkg-configFor Red Hat-based systems:
sudo yum install -y \
gcc-c++ \
cairo-devel \
pango-devel \
libjpeg-turbo-devel \
giflib-devel \
librsvg2-devel \
pkgconfigOn Fedora, use dnf instead of yum.
For Alpine (common in Docker):
apk add --no-cache \
build-base \
g++ \
cairo-dev \
pango-dev \
jpeg-dev \
giflib-dev \
librsvg-dev \
pkgconfigAfter installing dependencies, install canvas:
npm install canvasOr rebuild if already in package.json:
npm rebuild canvasFor Docker, add dependencies in your Dockerfile:
FROM node:20
# Install canvas dependencies
RUN apt-get update && apt-get install -y \
libcairo2-dev \
libpango1.0-dev \
libjpeg-dev \
libgif-dev \
librsvg2-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY package*.json ./
RUN npm ciFor multi-stage builds, remember that build dependencies are only needed in the build stage. The runtime stage needs the non-dev packages:
# Runtime stage only needs:
RUN apt-get update && apt-get install -y \
libcairo2 libpango-1.0-0 libjpeg62-turbo libgif7 librsvg2-2Consider using the canvas package's prebuilt binaries where available by checking their platform support documentation.
npm notice access token expired or revoked. Please try logging in again.
Token has expired - npm authentication failure
npm ERR! code EAI_AGAIN
How to fix "EAI_AGAIN" in npm
npm error code E403 npm error 403 Forbidden - PUT https://registry.npmjs.org/<package>
How to fix 'E403 Forbidden' error in npm
npm ERR! code EUSAGE npm ERR! Usage error
How to fix "npm ERR! code EUSAGE" in Node.js projects
npm ERR! code E401 npm ERR! 401 Unauthorized
How to fix "E401 Unauthorized" in npm