Microsoft Graph example snippets for JavaScript not working

SHCoder 21 Reputation points

I've been following this tutorial with this library, but the code snippets provided are producing errors. I have registered the app with Azure and followed the instructions, but when I run the code, it says SyntaxError: await is only valid in async functions and the top level bodies of modules at /script.js:74:20

Here's a relevant snippet of code, but if you have Replit, I would really appreciate it if you could collaborate with me on my Repl instead.

Replit link:


const msal = require('@azure/msal-node');  

// Create msal application object  
const cca = new msal.ConfidentialClientApplication(config);  

const REDIRECT_URI = "http://localhost:3000/redirect";  

const config = {  
    auth: {  
        clientId: "ebcb2e8c-4675-411f-a76e-25aafe0c026d",  
        authority: "",  
        // I wasn't sure if this meant the key value or the secret ID  
        clientSecret: "ee10b5ce-f9c4-460a-a402-064030841f86"  
    system: {  
        loggerOptions: {  
            loggerCallback(loglevel, message, containsPii) {  
            piiLoggingEnabled: false,  
            logLevel: msal.LogLevel.Verbose,  

// 1st leg of auth code flow: acquire a code  
app.get('/', (req, res) => {  
    const authCodeUrlParameters = {  
        scopes: [""],  
        redirectUri: REDIRECT_URI,  

    // get url to sign user in and consent to scopes needed for application  
    pca.getAuthCodeUrl(authCodeUrlParameters).then((response) => {  
    }).catch((error) => console.log(JSON.stringify(error)));  

// 2nd leg of auth code flow: exchange code for token  
app.get('/redirect', (req, res) => {  
    const tokenRequest = {  
        code: req.query.code,  
        scopes: [""],  
        redirectUri: REDIRECT_URI,  

    pca.acquireTokenByCode(tokenRequest).then((response) => {  
        console.log("\nResponse: \n:", response);  
    }).catch((error) => {  

try {  
 let userDetails = await client.api("/me").get();  
} catch (error) {  
 throw error;  
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,345 questions
0 comments No comments
{count} votes

Accepted answer
  1. 2022-02-03T12:52:02.277+00:00

    You can use "await" only inside a function which is "async".

    Here you can try remove 'await' from the code and try to run it , Hopes this resolves the issue.

0 additional answers

Sort by: Most helpful