Netlify function works with ntl dev, but returns 500 after ntl build

Hi! I’m running into an issue getting my function deployed. It works locally when I run ntl dev, but after running ntl build it doesn’t work, unless I delete the zip file it creates and re-run ntl dev.

If I run ntl build and then ntl deploy I see 0 public functions for my site.

Hitting the function locally from insomnia after ntl build and then ntl dev I see this in my terminal:

Request from ::1: POST /.netlify/functions/create-checkout
{"level":"error","message":"End - Error:"}
{"errorMessage":"Invalid or unexpected token","errorType":"SyntaxError","stackTrace":["\u0004\u0014","","","taxError: Invalid or unexpected token","wrapSafe (internal/modules/cjs/loader.js:1070:16)","Module._compile (internal/modules/cjs/loader.js:1120:27)","Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)","Module.load (internal/modules/cjs/loader.js:1000:32)","Function.Module._load (internal/modules/cjs/loader.js:899:14)","Module.require (internal/modules/cjs/loader.js:1042:19)","require (internal/modules/cjs/helpers.js:77:18)","Object._executeSync (/Users/beth/.nvm/versions/node/v12.16.2/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js:277:26)","Object.execute (/Users/beth/.nvm/versions/node/v12.16.2/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js:83:22)","executeSynchronousFunction (/Users/beth/.nvm/versions/node/v12.16.2/lib/node_modules/netlify-cli/src/utils/serve-functions.js:102:15)"],"level":"error"}
Response with status 500 in 3 ms.

My site is loving-benz-ece62b.netlify.app/, although thus far I’m still stuck at the local stage so I’m not sure how helpful that is.

My netlify.toml looks like

[build]
functions = ".netlify/functions"

My function is at .netlify/functions/create-checkout.js. I’ve replaced the file contents with a sample from netlify functions:create like:

// Docs on event and context https://www.netlify.com/docs/functions/#the-handler-method
const handler = async (event) => {
  try {
    const subject = event.queryStringParameters.name || 'World'
    return {
      statusCode: 200,
      body: JSON.stringify({ message: `Hello ${subject}` }),
      // // more keys you can return:
      // headers: { "headerName": "headerValue", ... },
      // isBase64Encoded: true,
    }
  } catch (error) {
    return { statusCode: 500, body: error.toString() }
  }
}
module.exports = { handler }

When I run ntl build it finds the functions:

Packaging Functions from .netlify/functions directory:
 - create-checkout.js

If I delete the zip files that are created, then re-run ntl dev everything behaves as expected and I get this response back:

{
  "message": "Hello World"
}

I found this post about functions not being recognized and updated my package.json scripts to have a build:lambda line, but it hasn’t made a difference afaict.

   "scripts": {
        "develop": "gatsby develop",
        "start": "gatsby develop",
        "build": "gatsby build",
        "serve": "gatsby serve",
        "clean": "gatsby clean",
        "build:lambda": "netlify-lambda build .netlify/functions"
      },

Pretty stuck on this, would love any help! Thank you :pray:

hey there,

sorry to hear this is giving you trouble.

Would you mind filing an issue here so that the team that works on netlify dev can take a look?

Hi @abethcrane

Thank you so much for writing in and sharing this with us.

Following up with what Perry shared, we have escalated this to our Engineers. Follow up steps and communication will be shared in this thread in efforts to streamline conversation. Please follow along there!

Thanks again for bringing this to our attention :slight_smile: