Home
Support Forums

Netlify functions invoke error: Invalid or unexpected token

This is a local install because I am having trouble with testing functions locally on another site. The error is the same on every try:

taxError: Invalid or unexpected token
wrapSafe (internal/modules/cjs/loader.js:979:16)
Module._compile (internal/modules/cjs/loader.js:1027:27)
Object.Module._extensions…js (internal/modules/cjs/loader.js:1092:10)
Module.load (internal/modules/cjs/loader.js:928:32)
Function.Module._load (internal/modules/cjs/loader.js:769:14)
Module.require (internal/modules/cjs/loader.js:952:19)
require (internal/modules/cjs/helpers.js:88:18)
Object._executeSync (/usr/local/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js:277:26)
/usr/local/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js:95:26
new Promise ()

The function invoked is the basic hello world example from the documentation. This seems like a global npm package error, not with local configs.

I have tried uninstalling netlify-cli and installing different versions. The same error every time. I have searched for hours if anyone have the same problem but no success.

Hey there, @moholdt :wave:

Thanks for reaching out and letting us know what you have tried so far. We can dig into this a bit further, but will just need a few more details first, Can you please share your netlify site that this is impacting, your most recently deploy log, as well as the name of the function you are using?

Thanks so much!

Hi.

I don’t think there is something wrong with any specific site I have on Netlify, rather a problem with Netlify CLI locally. As I stated the same error appears on my old sites as well as a fresh install or even just doing “netlify functions:serve” on a folder. Locally that is. The functions are running fine on production.

After a lot of trial and error, I found a way for my functions to work properly locally.

For some reason they don’t work when they are in the root folder of the functions folder, they need to be nested in their own respective folder to work. I have no idea why, but placing each function inside their own folder inside the functions folder worked.

Previous structure:
“functions/hello-world.js” did not work and gave me the error previously posted, every time
“functions/hello-world/hello-world.js” worked

I just tested after updating the CLI and also changing netlify.toml to:

[functions]
  directory = "functions"

made everything work again, even functions in the root of the folder. I’m not sure what the problem was, but still posting this in case any one else are having the same issue.