Skip to content

Getting error when adding layer to Lambda x86_64 and use Node.js 22.x #4

@nguyenminh1811

Description

@nguyenminh1811

I only have an index.js file that using Sharp lib and another file is package.json

// index.js


import { createCanvas } from 'canvas';
import sharp from 'sharp';
exports.handler = async (event) => {
  try {
    // 1. Create canvas and draw something
    const canvas = createCanvas(300, 300);
    const ctx = canvas.getContext('2d');

    ctx.fillStyle = 'red';
    ctx.fillRect(50, 50, 200, 200);

    // 2. Get canvas buffer
    const canvasBuffer = canvas.toBuffer('image/png');

    // 3. Use sharp to resize the image
    const resizedBuffer = await sharp(canvasBuffer)
      .resize(150, 150)
      .png()
      .toBuffer();

    // 4. Return base64-encoded image
    return {
      statusCode: 200,
      headers: { 'Content-Type': 'image/png' },
      body: resizedBuffer.toString('base64'),
      isBase64Encoded: true,
    };
  } catch (error) {
    console.error('Error processing image:', error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: 'Image processing failed' }),
    };
  }
};
// package.json


{
  "name": "lambda-image-handler",
  "version": "1.0.0",
  "main": "handler.js",
  "type": "module",
  "devDependencies": {
    "sharp": "^0.34.1"
  }
}

Status: Failed
Test Event Name: hello-world

Response:
{
"errorType": "Error",
"errorMessage": "Could not load the "sharp" module using the linux-x64 runtime\nPossible solutions:\n- Ensure optional dependencies can be installed:\n npm install --include=optional sharp\n- Ensure your package manager supports multi-platform installation:\n See https://sharp.pixelplumbing.com/install#cross-platform\n- Add platform-specific dependencies:\n npm install --os=linux --cpu=x64 sharp\n- Consult the installation documentation:\n See https://sharp.pixelplumbing.com/install",
"trace": [
"Error: Could not load the "sharp" module using the linux-x64 runtime",
"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=linux --cpu=x64 sharp",
"- Consult the installation documentation:",
" See https://sharp.pixelplumbing.com/install",
" at Object. (/opt/nodejs/node_modules/sharp/lib/sharp.js:121:9)",
" at Module._compile (node:internal/modules/cjs/loader:1730:14)",
" at Object..js (node:internal/modules/cjs/loader:1895:10)",
" at Module.load (node:internal/modules/cjs/loader:1465:32)",
" at Function._load (node:internal/modules/cjs/loader:1282:12)",
" at TracingChannel.traceSync (node:diagnostics_channel:322:14)",
" at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)",
" at Module.require (node:internal/modules/cjs/loader:1487:12)",
" at require (node:internal/modules/helpers:135:16)",
" at Object. (/opt/nodejs/node_modules/sharp/lib/constructor.js:10:1)"
]
}

Function Logs:
2025-06-05T03:55:13.065Z undefined ERROR Uncaught Exception {"errorType":"Error","errorMessage":"Could not load the "sharp" module using the linux-x64 runtime\nPossible solutions:\n- Ensure optional dependencies can be installed:\n npm install --include=optional sharp\n- Ensure your package manager supports multi-platform installation:\n See https://sharp.pixelplumbing.com/install#cross-platform\n- Add platform-specific dependencies:\n npm install --os=linux --cpu=x64 sharp\n- Consult the installation documentation:\n See https://sharp.pixelplumbing.com/install","stack":["Error: Could not load the "sharp" module using the linux-x64 runtime","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=linux --cpu=x64 sharp","- Consult the installation documentation:"," See https://sharp.pixelplumbing.com/install"," at Object. (/opt/nodejs/node_modules/sharp/lib/sharp.js:121:9)"," at Module._compile (node:internal/modules/cjs/loader:1730:14)"," at Object..js (node:internal/modules/cjs/loader:1895:10)"," at Module.load (node:internal/modules/cjs/loader:1465:32)"," at Function._load (node:internal/modules/cjs/loader:1282:12)"," at TracingChannel.traceSync (node:diagnostics_channel:322:14)"," at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)"," at Module.require (node:internal/modules/cjs/loader:1487:12)"," at require (node:internal/modules/helpers:135:16)"," at Object. (/opt/nodejs/node_modules/sharp/lib/constructor.js:10:1)"]}
INIT_REPORT Init Duration: 287.30 ms Phase: init Status: error Error Type: Runtime.Unknown
2025-06-05T03:55:13.283Z undefined ERROR Uncaught Exception {"errorType":"Error","errorMessage":"Could not load the "sharp" module using the linux-x64 runtime\nPossible solutions:\n- Ensure optional dependencies can be installed:\n npm install --include=optional sharp\n- Ensure your package manager supports multi-platform installation:\n See https://sharp.pixelplumbing.com/install#cross-platform\n- Add platform-specific dependencies:\n npm install --os=linux --cpu=x64 sharp\n- Consult the installation documentation:\n See https://sharp.pixelplumbing.com/install","stack":["Error: Could not load the "sharp" module using the linux-x64 runtime","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=linux --cpu=x64 sharp","- Consult the installation documentation:"," See https://sharp.pixelplumbing.com/install"," at Object. (/opt/nodejs/node_modules/sharp/lib/sharp.js:121:9)"," at Module._compile (node:internal/modules/cjs/loader:1730:14)"," at Object..js (node:internal/modules/cjs/loader:1895:10)"," at Module.load (node:internal/modules/cjs/loader:1465:32)"," at Function._load (node:internal/modules/cjs/loader:1282:12)"," at TracingChannel.traceSync (node:diagnostics_channel:322:14)"," at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)"," at Module.require (node:internal/modules/cjs/loader:1487:12)"," at require (node:internal/modules/helpers:135:16)"," at Object. (/opt/nodejs/node_modules/sharp/lib/constructor.js:10:1)"]}
INIT_REPORT Init Duration: 202.91 ms Phase: invoke Status: error Error Type: Runtime.Unknown
START RequestId: ec4649dc-1f11-41ab-9818-9fea79b57da1 Version: $LATEST
END RequestId: ec4649dc-1f11-41ab-9818-9fea79b57da1
REPORT RequestId: ec4649dc-1f11-41ab-9818-9fea79b57da1 Duration: 215.24 ms Billed Duration: 216 ms Memory Size: 1024 MB Max Memory Used: 102 MB Status: error Error Type: Runtime.Unknown

Request ID: ec4649dc-1f11-41ab-9818-9fea79b57da1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions