alexa / alexa-skills-kit-sdk-for-nodejs

The Alexa Skills Kit SDK for Node.js helps you get a skill up and running quickly, letting you focus on skill logic instead of boilerplate code.
Apache License 2.0
3.12k stars 736 forks source link

Cannot find module WebSocketClient.js #684

Closed matijagrcic closed 3 years ago

matijagrcic commented 3 years ago

Installed the ask-sdk-local-debug Configured the launch.json

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387

  //"program": "${workspaceFolder}/lambda/node_modules/ask-sdk-local-debug/",
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Alexa Skill (Node.js)",
      "type": "node",
      "request": "launch",
      "program": "${command:ask.debugAdapterPath}", //this didn't find the ask-sdk-local-debug so used "program": "${workspaceFolder}/lambda/node_modules/ask-sdk-local-debug/",
      "args": [
        "--accessToken",
        "${command:ask.accessToken}",
        "--skillId",
        "${command:ask.skillIdFromWorkspace}",
        "--handlerName",
        "handler",
        "--skillEntryFile",
        "${workspaceFolder}/lambda/index.js",
        "--region",
        "NA"
      ],
      "cwd": "${workspaceFolder}/lambda"
    }
  ]
}

Debugger gets attached all the args are passed correctly but it fails with

Error: Cannot find module 'C:\Git\Alexa\lambda\node_modules\ask-sdk-local-debug\dist\client\WebSocketClient.js'. Please verify that the package.json has a valid "main" entry

I've looked at that module and there is not WebSocketClient.js in the dist\client folder.

ShenChen93 commented 3 years ago

Hi @matijagrcic

Thanks for posting this issue. As you mentioned, "program": "${command:ask.debugAdapterPath}", //this didn't find the ask-sdk-local-debug so used "program": "${workspaceFolder}/lambda/node_modules/ask-sdk-local-debug/",, I wonder what's the error message you got while using the original config ?

Also, previously we haven't think up the use case of linking to the local-debug pacakge manually as what you did. After researching, I think it is a bug at the local-debug pacakge side, the main entry in package.json is not point to the correct file. I have a workaround which may unblock you for now, could you please try the following:

"program": "${workspaceFolder}/lambda/node_modules/ask-sdk-local-debug/dist/LocalDebuggerInvoker.js",

Thanks, Shen

matijagrcic commented 3 years ago

Hi @ShenChen-Amazon and thanks for replaying.

Thanks for posting this issue. As you mentioned, "program": "${command:ask.debugAdapterPath}", //this didn't find the ask-sdk-local-debug so used "program": "${workspaceFolder}/lambda/node_modules/ask-sdk-local-debug/",, I wonder what's the error message you got while using the original config ?

The exception is the following:

image

I've already tried to point to the LocalDebuggerInvoker.js as I was looking into the possible entry points given the one used doesn't exist, but then the exception is the following:

Exception has occurred: Error: ENOENT: no such file or directory, scandir 'C:\Git\Alexa\lambda\node_modules\bufferutil\build\Release'

I've then tried the following Cannot find module 'bufferutil' but same exception occurs.

Would you be able to fix the ask-sdk-local-debug and publish a package version that points to the correct main entry point? Let me know If there's any other info I can provide to further debug this.

ShenChen93 commented 3 years ago

Hi @matijagrcic

As the error message is not related to local-debug after you update the config, I think vscode could already find local-deubg package right now. For the new exception, since local-deubug and other SDK pacakges doesn't depend on bufferutil, I guess it's a dependency added by other packages or yourself. So looks like it's not only the local debug, but your vscode failed to find other dependencies as well. Probably you already tried delete node_modules and reinstall dependecies, does it work ? Could you please try to create a new Skill in a clean workspace, install dependencies under lambda folder and try the local debug again ?

Thanks, Shen

ShenChen93 commented 3 years ago

Hi @matijagrcic

Just a quick follow up. Are you still seeing this issue ?

matijagrcic commented 3 years ago

Hi @ShenChen-Amazon ,

I've used the following and then the debugger works fine (hits breakpoints).

"program": "node_modules/ask-sdk-local-debug/dist/LocalDebuggerInvoker.js",

Same issue was reported here https://github.com/alexa/ask-toolkit-for-vscode/issues/99#issuecomment-775202569

Where it fails is when I try to do anything with DynamoDB resulting in

~~~~ Error handled: {"name":"AskSdk.DynamoDbPersistenceAdapter Error"}

I'm using the following

import { DefaultApiClient, LambdaHandler, SkillBuilders } from "ask-sdk-core";
import { DynamoDB } from "aws-sdk";
.withPersistenceAdapter(
        new ddbAdapter.DynamoDbPersistenceAdapter({
            tableName: process.env.DYNAMODB_PERSISTENCE_TABLE_NAME,
            createTable: false,
            dynamoDBClient: new DynamoDB({apiVersion: 'latest', region: process.env.DYNAMODB_PERSISTENCE_REGION})
})
ShenChen93 commented 3 years ago

Hi @matijagrcic

Glad to hear that the workaround solve your issue. And for sure we will investigate whether the vscode command failed to retrieve the local-deub package path.

Then for the dynamodb issue, the error message is not very helpful, and my guess is the permission issue. When your lambda code is exectued at aws lambda instead of local debug, the lambda use IAM role to access the dynamodb. However, when you use local debug, your local lambda may not have the permission to access. You could either pass in your aws accessKeyId and secretAccessKey while constrcuting your dynamoDBClient, or you could use AWS CLI configure command to setup the credential locally(the second approach is recomended since you don't need to change skill code). Let me know if it fix your issue.

Also for use case such related to persistent layer, ideally the local skill should not interact with a real table. Instead, it would be great if the local debug package can help create a table locally. This feature is in the local-debug backlog.

Thanks, Shen

matijagrcic commented 3 years ago

HI @ShenChen-Amazon ,

I'm familiar with IAM role(s) etc and I also think it's because of the permissions but when I ran the

ask configure

and followed the steps it fails on the step where it opens the IAM dashboard, full terminal log below along with what's displayed in the IAM dashboard web page

PS C:\Git\Alexa\lambda> ask configure
This command will configure the ASK CLI with a profile associated with your Amazon developer credentials.
------------------------- Step 1 of 2 : ASK CLI Configuration -------------------------
? Please create a new profile or overwrite the existing profile.

[Warn]: ASK CLI uses authorization code to fetch LWA tokens. Do not share neither your authorization code nor access tokens.
Switch to "Login with Amazon" page and sign-in with your Amazon developer credentials.
If your browser did not open the page, try to run the command again with "--no-browser" option.

ASK Profile "AlexaASK" was successfully created. The details are recorded in ask-cli config file (.ask/cli_config) located at your **HOME** folder.
Vendor ID set as XXXXXXXXXXXXX.

------------------------- Step 2 of 2 : Associate an AWS Profile with ASK CLI -------------------------
[Warn]: ASK CLI will create an IAM user and generate corresponding access key id and secret access key. Do not share neither of them.
? Do you want to link your AWS account in order to host your Alexa skills? Yes
? Please choose from the following existing AWS profiles or create a new one. Create new profile
? Please provide your AWS profile name:  AlexaAWS

Complete the IAM user creation with required permissions from the AWS console, then come back to the terminal.

Please fill in the "Access Key ID" and "Secret Access Key" from the IAM user creation final page.
? AWS Access Key ID:  

IAM dashboard

We encountered the following errors while processing your request:

User: arn:aws:sts::XXXXXXXXXXX:assumed-role/VoiceHubSSORole/VoiceHubSSORole is not authorized to perform: iam:GetAccountSummary on resource: *
User: arn:aws:sts::XXXXXXXXXXX:assumed-role/VoiceHubSSORole/VoiceHubSSORole is not authorized to perform: iam:ListAccountAliases on resource: *
IAM resources
We encountered the following errors while processing your request:

User: arn:aws:sts::XXXXXXXXXXX:assumed-role/VoiceHubSSORole/VoiceHubSSORole is not authorized to perform: iam:GetAccountSummary on resource: *
User: arn:aws:sts::XXXXXXXXXXX:assumed-role/VoiceHubSSORole/VoiceHubSSORole is not authorized to perform: iam:ListAccountAliases on resource: *

So how do I get the accessKeyIdand secretAccessKeywhen using Alexa Hosted Skill?

This isn't a problem when I use my own resources on my personal AWS account.

ShenChen93 commented 3 years ago

Hi @matijagrcic ,

If you are using hosted skill, then there's no way to get the credential so far. Hosted skill service manage all the aws resources and they haven't provide way for developer to retrieve. I am sorry for the inconveience, but this is a good feature request for hosted skill team, I will track it with their team. Closing this issue from our side for now, as the original issue is tracked at extension repo. Please feel free to reopen if you have any questions :)

Thanks, Shen

matijagrcic commented 3 years ago

Hi @ShenChen-Amazon ,

Yeah, makes sense. Let me help you with features requests, and please let me know where should i open them:

allow hosted skill to define a "outDir": "dist" instead of having a hardcoded index.js

allow local debug for hosted alexa skill when using DynamoDB

Both of the above listed really undermine the whole "hosted skill" mentality.

ShenChen93 commented 3 years ago

Hi @matijagrcic

Just knowed that there's a solution that could help local debug persistent adapter: https://github.com/alexa/alexa-cookbook/tree/master/tools/LocalPersistence, hope it could help for now.

For allow local debug for hosted alexa skill when using DynamoDB, you could open an new issue at this repo. For allow hosted skill to define a "outDir": "dist" instead of having a hardcoded index.js, it would be great if you could create one at Alexa developer forum.

Thanks, Shen