Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

288
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda