Amit-A / formidable-serverless

Enables use of formidable (node.js module for parsing form data, especially file uploads) in serverless environments.
https://www.npmjs.com/package/formidable-serverless
MIT License
35 stars 2 forks source link

"TypeError: req.on is not a function", #12

Open RickDir opened 4 months ago

RickDir commented 4 months ago

Hi, @Amit-A ,

AWS lambda is throwing:

> 2024-02-21T08:21:47.729Z  d74793a1-1596-4182-9ce5-6eb0370f8941    ERROR   Invoke Error    
> {
>     "errorType": "TypeError",
>     "errorMessage": "req.on is not a function",
>     "stack": [
>         "TypeError: req.on is not a function",
>         "    at formidable.IncomingForm.parse (/var/task/node_modules/formidable-serverless/lib/index.js:39:7)",
>         "    at response.statusCode (/var/task/index.js:89:12)",
>         "    at new Promise (<anonymous>)",
>         "    at exports.handler (/var/task/index.js:88:12)"
>     ]
> }

I have a really plain lambda so I’m not sure what I’m missing.

My AWS lambda config:

Compatible architectures - Both: • x86_64 • arm64

Compatible runtimes: • nodejs 20.x

console.log('Node version is: ' + process.version);
console.log(process.versions);

Shows in CloudWatch:

Node version is: v20.11.0
{
node: '20.11.0',
acorn: '8.11.2',
ada: '2.7.4',
ares: '1.20.1',
base64: '0.5.1',
brotli: '1.0.9',
cjs_module_lexer: '1.2.2',
cldr: '43.1',
icu: '73.2',
llhttp: '8.1.1',
modules: '115',
napi: '9',
nghttp2: '1.58.0',
openssl: '3.1.4',
simdutf: '4.0.4',
tz: '2023c',
undici: '5.27.2',
unicode: '15.0',
uv: '1.46.0',
uvwasi: '0.0.19',
v8: '11.3.244.8-node.17',
zlib: '1.2.13.1-motley-5daffc7'
}

Here is my index.js:

console.log('Node version is: ' + process.version);
console.log(process.versions);

const isCustom = require('/opt/nodejs/myCustom');

const formidable = require('formidable-serverless');
const http = require('http');
const util = require('util');

const { S3Client } = require("@aws-sdk/client-s3");
const s3 = new S3Client({ region: "us-east-1" });

exports.handler = async(event) => {
    const res = await isCustom.handler(event);
    if (!res.isValid) { return res.failed; }

    const form = new formidable.IncomingForm();

    return new Promise((resolve, reject) => {
      form.parse(event, (err, fields, files) => {
        if(err) return reject(err);

        console.log(fields);  
        console.log(files);  

        resolve({status: 'ok'});
      });
    });

    return res.def; 
};

I have an easily reproducible setup: • Windows 10 with VS Code. • Just installed node 20.11.1 LTS from nodejs.org using the big button which downloads: node-v20.11.1-x64.msi • In my VS Code terminal: npm install npm@latest -g • So now npm -v Shows version: 10.4.0 node -v Shows version: v20.11.1

  1. Created my folder "c:\git\lmbd-202-api" and added in my "index.js" as shown above.
  2. In VS Code: File, Open [that] folder.
  3. In VS Code: Terminal, New terminal and type: npm init -y
  4. Then type: npm install formidable-serverless

The output was: npm WARN deprecated formidable@1.2.6: Please upgrade to latest, formidable@v2 or formidable@v3! Check these notes: https://bit.ly/2ZEqIau

Is there a npm command to just upgrade the parts of formidable that are needed (like your install only pulls 43k) or do I have to pull the entire 400k? Note, I still received the same error after downloading all of it. npm install formidable@v3

  1. Selected all the files in my "lmbd-202-api" folder, right mouse click: Send to: Compressed zip folder, and name it "lmbd-202-api.zip."
  2. In the AWS Lambda console, on the “Code source” page, click Upload from, .zip file. Select the lmbd-202-api.zip. Note "lmbd-202-api" is also the exact name of my Lambda function.
  3. In the AWS Lambda console, add a space after any semicolon so the Deploy button becomes active; Then click it.
  4. Hitting the end point with a Postman PUT with multipart Form-Data that contains a 1k json file and two 50k images throws the 500 level error.

Note, my CloudFront origin is passing the request to my API Gateway which has Lambda Proxy Integration on and is in turn passing the request to my Lambda. Note, I logged the entire event to CloudWatch and the last line in the event is: isBase64Encoded: false.

Thank you