Getting Started
Installation
If you’re running on a supported platform, installation should be as simple as:
npm install skia-canvas
This will download a pre-compiled library from the project’s most recent release.
pnpm
If you use the pnpm
package manager, it will not download skia-canvas
's platform-native binary unless you explicitly allow it. You can do this interactively via the ‘approve builds’ command (note that you need to press <space>
to toggle the selection and then <enter>
to proceed):
pnpm install skia-canvas
pnpm approve-builds
In non-interactive scenarios (like building via CI), you can approve the build step when you add skia-canvas
to your project:
pnpm install skia-canvas --allow-build=skia-canvas
Alternatively, you can add a pnpm.onlyBuiltDependencies
entry to your package.json
file to mark the build-step as allowed:
{
"pnpm": {
"onlyBuiltDependencies": ["skia-canvas"]
}
}
Platform Support
Skia Canvas runs on Linux, macOS, or Windows as well as serverless platforms like Vercel and AWS Lambda. Precompiled versions of the library’s native code will be automatically downloaded in the appropriate architecture (arm64
or x64
) when you install it via npm.
The underlying Rust library uses N-API v8 which allows it to run on all currently supported Node.js releases, and it is backward compatible with versions going back to v12.22+, v14.17+, v15.12+, and v16+.
Linux
The library is compatible with Linux systems using glibc 2.28 or later as well as Alpine Linux and the musl C library it favors. It will make use of the system’s fontconfig
settings in /etc/fonts
if they exist but will otherwise fall back to using a placeholder configuration, looking for installed fonts at commonly used Linux paths.
Docker
If you are setting up a Dockerfile that uses node
as its basis, the simplest approach is to set your FROM
image to one of the (Debian-derived) defaults like node:lts
, node:22
, node:24-bookworm
, or simply:
FROM node
If you wish to use Alpine as the underlying distribution, you can start with something along the lines of:
FROM node:alpine
AWS Lambda
Skia Canvas depends on libraries that aren't present in the standard Lambda runtime. You can add these to your function by uploading a ‘layer’ (a zip file containing the required libraries and node_modules
directory) and configuring your function to use it.
Detailed AWS instructions
Adding the Skia Canvas layer to your AWS account
- Look in the Assets section of Skia Canvas’s current release and download the
aws-lambda-x64.zip
oraws-lambda-arm64.zip
file (depending on your architecture) but don’t decompress it - Go to the AWS Lambda Layers console and click the Create Layer button, then fill in the fields:
- Name:
skia-canvas
(or whatever you want) - Description: you might want to note the Skia Canvas version here
- Compatible architectures: select x86_64 or arm64 depending on which zip you chose
- Compatible runtimes: select Node.js 22.x (and/or 20.x)
- Click the Choose file button and select the zip file you downloaded in Step 1, then click Create
Alternatively, you can use the aws
command line tool to create the layer. This bash script will fetch the skia-canvas version of your choice and make it available to your Lambda functions.
#!/usr/bin/env bash
VERSION=3.0 # the skia-canvas version to include
PLATFORM=arm64 # arm64 or x64
curl -sLO https://github.com/samizdatco/skia-canvas/releases/download/v${VERSION}/aws-lambda-${PLATFORM}.zip
aws lambda publish-layer-version \
--layer-name "skia-canvas" \
--description "Skia Canvas ${VERSION} layer" \
--zip-file "fileb://aws-lambda-${PLATFORM}.zip" \
--compatible-runtimes "nodejs20.x" "nodejs22.x" \
--compatible-architectures "${X/#x/x86_}"
Using the layer in a Lambda function
You can now use this layer in any function you create in the Functions console. After creating a new function, click the Add a Layer button and you can select your newly created Skia Canvas layer from the Custom Layers layer source.
Note that the layer only includes Skia Canvas and its dependencies—any other npm modules you want to use will need to be bundled into your function. To prevent the skia-canvas
module from being doubly-included, make sure you add it to the devDependencies
section (not the regular dependencies
section) of your package.json file.
Next.js / Webpack
If you are using a framework like Next.js that bundles your server-side code with Webpack, you'll need to mark skia-canvas
as an ‘external’, otherwise its platform-native binary file will be excluded from the final build. Try adding these options to your next.config.ts
file:
const nextConfig: NextConfig = {
serverExternalPackages: ['skia-canvas'],
webpack: (config, options) => {
if (options.isServer){
config.externals = [
...config.externals,
{'skia-canvas': 'commonjs skia-canvas'},
]
}
return config
}
};
Compiling from Source
If prebuilt binaries aren’t available for your system you’ll need to compile the portions of this library that directly interface with Skia.
Start by installing:
- A recent version of
git
(older versions have difficulties with Skia's submodules) - The Rust compiler and cargo package manager using
rustup
- A C compiler toolchain (either LLVM/Clang or MSVC)
- Python 3 (used by Skia's build process)
- The Ninja build system
- On Linux: Fontconfig and OpenSSL
Detailed instructions for setting up these dependencies on different operating systems can be found in the ‘Building’ section of the Rust Skia documentation. The Dockerfiles in the containers directory may also be useful for identifying needed dependencies. Once all the necessary compilers and libraries are present, running npm run build
will give you a usable library (after a fairly lengthy compilation process).
Global Settings
There are a handful of settings that can only be configured at launch and will apply to all the canvases you create in your script. The sections below describe the different environment variables you can set to make global changes. You can either set them as part of your command line invocation, or place them in a
.env
file in your project directory and use Node 20's--env-file
argument to load them all at once.
Multithreading
When rendering canvases in the background (e.g., by using the asynchronous toFile or toBuffer methods), tasks are spawned in a thread pool managed by the rayon library. By default it will create up to as many threads as your CPU has cores. You can see this default value by inspecting any Canvas object's engine.threads
property. If you wish to override this default, you can set the SKIA_CANVAS_THREADS
environment variable to your preferred value.
For example, you can limit your asynchronous processing to two simultaneous tasks by running your script with:
SKIA_CANVAS_THREADS=2 node my-canvas-script.js
Argument Validation
There are a number of situations where the browser API will react to invalid arguments by silently ignoring the method call rather than throwing an error. For example, these lines will simply have no effect:
ctx.fillRect(0, 0, 100, "october")
ctx.lineTo(NaN, 0)
Skia Canvas does its best to emulate these quirks, but allows you to opt into a stricter mode in which it will throw TypeErrors in these situations (which can be useful for debugging).
Set the SKIA_CANVAS_STRICT
environment variable to 1
or true
to enable this mode.