0

I'm writing a lambda function where I need to import discord.js to get the number of users in a discord server.

This is the basic skeleton of the function, where I've removed the code, as just importing the discord.js node module generates the error:

const djs = require('discord.js');

exports.lambdaHandler = async (event, context) => {
    try {

    } catch (err) {
        console.log(err);
        return err;
    }

    return null;
};

I get the following error:

{
  "errorType": "Runtime.UserCodeSyntaxError",
  "errorMessage": "SyntaxError: Unexpected token '??='",
  "trace": [
    "Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '??='",
    "    at _loadUserApp (/var/runtime/UserFunction.js:200:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:242:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1085:14)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
    "    at Module.load (internal/modules/cjs/loader.js:950:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
    "    at internal/main/run_main_module.js:17:47"
  ]
}

On the following thread Why do I get a UserCodeSyntaxError when I have no syntax error in my code? I see they got the same error, but because they were using the import. They suggest using require instead, which I'm using and I get the same error.

They also suggest that it could be the version of nodejs. But I'm using version 14.x

Here's my template.yaml file:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'lambda-discord-get-member-count

  Sample SAM Template for lambda-discord-get-member-count

  '
Globals:
  Function:
    Timeout: 3
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: HelloWorldFunction
      Handler: app.lambdaHandler
      Runtime: nodejs14.x
      Architectures:
      - x86_64
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get
    Metadata:
      SamResourceId: HelloWorldFunction
Outputs:
  HelloWorldApi:
    Description: API Gateway endpoint URL for Prod stage for Hello World function
    Value:
      Fn::Sub: https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/
  HelloWorldFunction:
    Description: Hello World Lambda Function ARN
    Value:
      Fn::GetAtt:
      - HelloWorldFunction
      - Arn
  HelloWorldFunctionIamRole:
    Description: Implicit IAM Role created for Hello World function
    Value:
      Fn::GetAtt:
      - HelloWorldFunctionRole
      - Arn

  • _"But I'm using version 14.x"_ That's the problem. Logical nullish assignment (`??=`) is only available from node.js v15. – Zsolt Meszaros Jan 08 '22 at 16:08
  • 1
    Thank you for the quick answer. You pointed me in the right direction. The discord.js version I'm using is v13 which requires node v16.6. I Used an older version of discord.js (v12.5.3) which requires nodejs 12 and it worked. thank you again – Carlos de la Lama-Noriega Jan 08 '22 at 21:02

0 Answers0