Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

286
Vistas
AWS Lambda fails when I import an image. Runtime.UserCodeSyntaxError

Context

I am working on a Lambda function in my index.js file to help me add watermark on images using Sharp.

This is the error I am getting.

2022-04-01T20:33:49.269Z    undefined   ERROR   Uncaught Exception  {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Invalid or unexpected token","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Invalid or unexpected token","   
   at _loadUserApp ._loadUserApp (/var/runtime/UserFunction.js:200:13 undefined)","   
   at Object.module.exports.load (/var/runtime/UserFunction.js:242:17 undefined)","   
   at Object.<anonymous> (/var/runtime/index.js:43:30 undefined)","   
   at Module._compile (internal/modules/cjs/loader.js:1085:14 undefined)","   
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10 undefined)","   
   at Module.load (internal/modules/cjs/loader.js:950:32 undefined)","   
   at Function.Module._load (internal/modules/cjs/loader.js:790:12 undefined)","   
   at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12 undefined)","    at internal/main/run_main_module.js:17:47"]}

The Culprit

I realized the issue was happening when I imported a .png file.

const sharp = require('sharp');
const { S3 } = require('aws-sdk');
const stream = require('stream');
const playButtonPng = require('./assets/playButton.png'); // When this is commented out, the Lambda works

I thought about maybe "pulling" this image from S3, but it becomes complex since Sharp requires a "real image."

So my question is: is it possible to import a local .png in a lambda function? I tried investigating but did not find any results.

Other Things I've tried:

  • Using CommonJS imports
  • Configuring Lambda to use Node 14.x as I am using v14.18.3 locally
  • Double checking syntax. Using Babel to compile.
  • Adjusting the sharp node_module for production. (I don't think this was ever the issue).

I've also looked at these threads, which suggested

  • Why do I get a UserCodeSyntaxError when I have no syntax error in my code?
  • https://github.com/aws/aws-sam-cli/issues/3361
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

load PNG file by readFile / readFileSync available in fs module.

var fs = require('fs')
const playButtonPng = fs.readFileSync('./assets/playButton.png')
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda