cli
cli copied to clipboard
Function response should have empty body if async keyword is missing
- Do you want to request a feature or report a bug?
Bug
- What is the current behavior?
There is a difference in behaviour between local and production if you miss the async
keyword in a lambda function. When running netlify dev
, the response body is output. However, on production, the body is empty. This is confusing, and the local behaviour should match production.
- If the current behavior is a bug, please provide the steps to reproduce.
Using this function - note, it is missing the async
keyword:
exports.handler = function(event) {
console.log(event);
return {
statusCode: 200,
body: 'Hello world'
};
};
When I run this locally using netlify dev
, I see Hello world
output when I hit the local endpoint. However, when it is uploaded to the production environment, the function runs (the console.log
output is visible in the function log), but the response body is empty.
- What is the expected behavior?
Local netlify dev
should match the production behaviour, i.e. there should be no response body output from this function.
(Note that if the first line is changed to add the async
keyword, i.e. exports.handler = async function(event) {
, then both local and production behave the same - i.e. you do see the "Hello world" output in the body. Not sure why removing the async
keyword results in this change in behaviour, as the function doesn't await anything, but this is a difference in observed behaviour between local and production environments, and it confused me for a while)
- Local Environment Information
────────────────────┐
Environment Info │
────────────────────┘
System:
OS: macOS 10.15.5
CPU: (8) x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
Binaries:
Node: 12.16.3 - /usr/local/bin/node
npm: 6.14.4 - /usr/local/bin/npm
Browsers:
Chrome: 84.0.4147.105
Firefox: 56.0
Safari: 13.1.1
npmGlobalPackages:
netlify-cli: 2.58.0
netlify-cli/2.58.0 darwin-x64 node-v12.16.3
Can confirm I've run into similar problems due to the mismatched behavior between local behavior and prod. The async
keyword appears to be unnecessary in some cases when running locally, which can lead to difficult to track bugs for users less familiar with the necessary async
syntax even for synchronous Netlify functions.
Thread documenting my experience here: https://answers.netlify.com/t/functions-error-decoding-lambda-response-invalid-status-code-returned-from-lambda-0/53861