Netlify DEV: TypeError: res.writeHead is not a function

Hello,

I get this error whenever I call the netlify function. sometimes it works and sometimes not and I get this back

Request from ::ffff:127.0.0.1: GET /.netlify/functions/getS3URL?reqType=get
Response with status 200 in 2919 ms.

C:\...AppData\Roaming\npm\node_modules\netlify-cli\node_modules\netlify-redirector\lib\redirects.js:1...

TypeError: res.writeHead is not a function
    at ProxyServer.<anonymous> (AppData\Roaming\npm\node_modules\netlify-cli\src\utils\proxy.js:318:9)      
    at ProxyServer.emit (Roaming\npm\node_modules\netlify-cli\node_modules\eventemitter3\index.js:204:33)
    at Socket.onOutgoingError (AppData\Roaming\npm\node_modules\netlify-cli\node_modules\http-proxy\lib\http-proxy\passes\ws-incoming.js:157:16)
    at Socket.emit (node:events:525:35)
    at Socket.emit (node:domain:489:12)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Angular:

api.service:

getS3URL(){
  let queryParams = new HttpParams();
  return this.http.get(`${baseUrl}getS3URL`,{ params: queryParams }).pipe();
 }

in the component:

 getS3URL() {
    if (this.product.images[0]) {
      this.api.getS3URL().subscribe({
        next: (value: any) => { console.log('resp: ',value)}
        , error: (err) => { console.log('error', err) }
      })
    }
  }

Netlify function:

import dotenv from 'dotenv'
import aws from 'aws-sdk'
import crypto from 'crypto'
import { promisify } from "util"
import { Response } from '@netlify/functions/dist/function/response'
dotenv.config()
const randomBytes = promisify(crypto.randomBytes)
const region = "us-east-1"
const bucketName = "-----"
const accessKeyId = process.env['AWS_ACCESS_KEY_ID']
const secretAccessKey = process.env['AWS_SECRET_ACCESS_KEY']
const s3 = new aws.S3({
	region,
	accessKeyId,
	secretAccessKey,
	signatureVersion: 'v4'
})
exports.handler = async (event: any, context: any, callback: any) => {
	let resp: Response
	let putURL: string = ''
	try {
		const rawBytes = await randomBytes(16)
		const imageName = rawBytes.toString()
		var params = { Bucket: bucketName, Key: imageName, Expires: 60 };
		var promise = await s3.getSignedUrlPromise('putObject', params).then(value=>putURL=value)
		resp = {
			statusCode: 200,
			body: JSON.stringify({
				URL:putURL 
			})
		}
	} catch (err: any) {
		console.log(err.stack)
		resp = {
			statusCode: 400,
			body: err.stack
		};
	}
	return resp
}

I’m using in my project other netlify functions to do some other api requests and they are working just fine.
Used versions:
angular 14 as framework
netlify: 12.0.1
netlify-cli: 12.0.11

Thanks

Would you be able to send a reproduction?

Hi @hrishikesh ,

hier is the git repo

Hey @Benki2002,

That repository seems to work fine, unless I’m missing something.

I clicked on the button and got this in my terminal:

which seems to me like the CLI is working fine as the error is coming from AWS.

Did you try another device?