Runtime.UserCodeSyntaxError and Unexpected token '.'

When running the autotask from this repository I get the following error:

Error
SyntaxError: Unexpected token '.'
Logs
AUTOTASK START
2022-08-25T13:18:05.677Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected token '.'","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '.'","    at _loadUserApp (/var/runtime/UserFunction.js:98:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:999:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)","    at Module.load (internal/modules/cjs/loader.js:863:32)","    at Function.Module._load (internal/modules/cjs/loader.js:708:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)","    at internal/main/run_main_module.js:17:47"]}
2022-08-25T13:18:06.776Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected token '.'","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '.'","    at _loadUserApp (/var/runtime/UserFunction.js:98:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:999:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)","    at Module.load (internal/modules/cjs/loader.js:863:32)","    at Function.Module._load (internal/modules/cjs/loader.js:708:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)","    at internal/main/run_main_module.js:17:47"]}
END RequestId: 1f754eb3-7805-447e-bafe-c8f1ffd78f1a
AUTOTASK COMPLETE
Unknown application error occurred
Runtime.UserCodeSyntaxError

I've been using Defender for quite some time and have never seen this before. I've verified that this code works locally.

Any ideas?

Good day @flaco_jones , thanks for reaching out.

It seems to be a syntax error from the code uploaded in the autotask.

The fact that you can run the code locally and not in the autotask might be du to a node version mismatch, in Autotasks node 12 is used as the runtime for now. So you need to make sure your code is readable for node 12, I would suggest using node 12 to run the code locally to make sure there is no syntax error for this node version.

For example, looking at this file from the repository you shared the code is written with optional chaining wich is probably interpreted as a syntax error for node 12 as it was supported only with node 14.

Hope it helps, keep autotasking!

Thanks for your quick reply, yes I suspect it might be. I, like many, have yet to achieve perfectly bug free code :slight_smile:

This was a hard one for me to begin debugging however because there's no reference to linenumber, only to the internal/modules/run_main.js:60:12

I'll try to match Node versions and post the outcome here.

I'm also going to downgrade the Node version in my dev images for this Autotask to 12 as well. Good to know!

You are a god!

After downgrading to Node v 12 I get:

/Users/alo/OpenQ-Fullstack/OpenQ-Bounty-Actions-Autotask/openq-api/getCategory.js:3
        if (type === "0" || labels?.some(label => label === "prime")) {
                                   ^

SyntaxError: Unexpected token '.'

So folks, NO OPTIONAL CHAINING!

Runtime and dependencies issues are always tricky ones!

Also a side suggestion, you could use a transpiler to write full-featured javascript and transpile it down to node 12 compatible code.

Have a good day!