User Provisioning Guide - SyntaxError: await is only valid in async functions and the top level bodies of modules

Hi,
I am learning this API guide - User Provisioning Guide

It mentions:
To use the alternative user-provisioning-service.js script, which enables you to run the script's create-user code as a REST-based service, use the following procedure:

a. Change to the src directory and then run node user-provisioning-service.js. b. After the service is started

I used the code sample on GitHub. GitHub - MyPureCloud/user-provisioning-scripts-js: User provisioning scripts that will be used for our user management developer tutorials.

I tried the same command and encountered an error as follows.

C:\git_localdir_2024\github_junxu_user-provisioning-scripts-js\src>node user-provisioning-service.js
C:\git_localdir_2024\github_junxu_user-provisioning-scripts-js\src\user-provisioning-service.js:14
const response = await authApiProxy.authenticate(
^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
at internalCompileFunction (node:internal/vm:77:18)
at wrapSafe (node:internal/modules/cjs/loader:1288:20)
at Module._compile (node:internal/modules/cjs/loader:1340:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49

Node.js v20.11.1

Can you please help check what the root cause of it? How to address it? Thank you!

James Xu

You can find information about this JavaScript error here: SyntaxError: await is only valid in async functions, async generators and modules - JavaScript | MDN

The repo doesn't state what node version it was written for, but based on the commit dates it was likely using node 8 or 10. Enforcement/behavior of that rule likely changed since then.

I haven't seen that codebase before (and the person who wrote it is no longer with Genesys), but it looks like you can probably just remove the await statement and the following console log. It will probably create a race condition in getting the SDK authorized, so I don't know what effects that will have. But maybe just giving the service a few seconds to initialize and let the auth process complete will work. You can/should convert that to a promise so you can log success/failure.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.