Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 为什么调试会中断对AWS::Laymba::LayerVersion的访问;sam调用本地“;?_Typescript_Amazon Web Services_Visual Studio Code_Aws Lambda_Vscode Debugger - Fatal编程技术网

Typescript 为什么调试会中断对AWS::Laymba::LayerVersion的访问;sam调用本地“;?

Typescript 为什么调试会中断对AWS::Laymba::LayerVersion的访问;sam调用本地“;?,typescript,amazon-web-services,visual-studio-code,aws-lambda,vscode-debugger,Typescript,Amazon Web Services,Visual Studio Code,Aws Lambda,Vscode Debugger,这项工作: sam local invoke-t template.local.yaml-e events/event-timezone.json GetTimezoneFunction 尝试使用Visual Studio代码进行调试时,这不起作用: sam local invoke-t template.local.yaml-e events/event-timezone.json-d 5858 GetTimezoneFunction 不同之处在于,在第二个示例中,我得到一个错误,没有找到在l

这项工作:

sam local invoke-t template.local.yaml-e events/event-timezone.json GetTimezoneFunction

尝试使用Visual Studio代码进行调试时,这不起作用:

sam local invoke-t template.local.yaml-e events/event-timezone.json-d 5858 GetTimezoneFunction

不同之处在于,在第二个示例中,我得到一个错误,没有找到在lambda函数使用的单独层中定义的npm“axios”模块

Error: Cannot find module 'axios'
Require stack:
 - /var/task/get-timezone.js
 - /var/runtime/UserFunction.js
 - /var/runtime/index.js
我所能想到的是,可能通过尝试使用调试器,该层的资源没有复制到Docker运行时环境,或者某些文件搜索路径被破坏

我有另一个lambda函数,它不需要层中的任何东西,无论是否使用调试器都可以正常工作

具有
getTimeZone
函数的文件以以下开头:

从“axios”导入axios

这是
template.local.yaml
的相关部分:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Sample

Globals:
  Function:
    Timeout: 30

Resources:
  SampleCommonLayer:
    Type: AWS::Lambda::LayerVersion
    Properties:
      CompatibleRuntimes:
        - nodejs12.x
      Content: dependencies
      Description: Sample Common LayerVersion
      LayerName: SampleCommonLayer

  GetTimezoneFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: dist/get-timezone
      Handler: get-timezone.getTimezone
      Runtime: nodejs12.x
      Layers:
        - !Ref SampleCommonLayer
      Events:
        GetTimezone:
          Type: Api
          Properties:
            Path: /get-timezone
            Method: get

Outputs:
  GetTimezoneApi:
    Description: "API Gateway endpoint URL for Prod stage for getTimezone function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/get-timezone/"
  GetTimezoneFunction:
    Description: "getTimezone Lambda Function ARN"
    Value: !GetAtt GetTimezoneFunction.Arn
  GetTimezoneFunctionIamRole:
    Description: "Implicit IAM Role created for getTimezone function"
    Value: !GetAtt GetTimezoneFunctionRole.Arn
这是我的
launch.json

{
  "version": "0.2.0",
  "configurations": [{
      "name": "Debug get-timezone",
      "type": "node",
      "request": "attach",
      "address": "localhost",
      "port": 5858,
      "localRoot": "${workspaceRoot}/dist",
      "remoteRoot": "/var/task",
      "protocol": "inspector",
      "stopOnEntry": false,
      "preLaunchTask": "local-tz-debug"
    },
    ...
  ]
}

到目前为止,在搜索此问题的答案时,我只发现了许多未回答的问题和一些不完全相同的问题。

我让我的一个lambda函数从
process.env
中转储所有环境变量,并发现启用调试时(通过
-D5858
)之间存在一些差异如果没有,最值得注意的是,启用调试时没有定义
节点路径

我想我曾经尝试过一次修复
NODE\u path
,基于我在其他地方读到的类似问题,但似乎没有帮助

然而,现在我正在转储环境变量,我可以看到我在
env.json
文件中定义
NODE\u PATH
的尝试没有任何效果。这时我发现SAM忽略了任何没有在我的模板中明确定义的环境变量,无论是全局变量还是每个lambda

这解决了这个问题:

Globals:
  Function:
    Timeout: 30
    Environment:
      Variables:
        NODE_PATH: /opt/nodejs/node12/node_modules:/opt/nodejs/node_modules:/var/runtime/node_modules
一旦我解决了这个问题,它发现我需要调整我的
launch.json
,以使调试器断点能够为TypeScript工作:

{
  "version": "0.2.0",
  "configurations": [{
      "name": "Debug get-timezone",
      "type": "node",
      "request": "attach",
      "address": "localhost",
      "port": 5858,
      "localRoot": "${workspaceRoot}/dist/get-timezone", // <- More specific path needed
      "remoteRoot": "/var/task",
      "protocol": "inspector",
      "stopOnEntry": false,
      "preLaunchTask": "local-tz-debug",
      "outFiles": [ // <- Added this
        "${workspaceRoot}/dist/get-timezone/**/*.js"
      ],
      "sourceMaps": true // <- Added this
    },
    ...
  ]
}
{
“版本”:“0.2.0”,
“配置”:[{
“名称”:“调试获取时区”,
“类型”:“节点”,
“请求”:“附上”,
“地址”:“本地主机”,
“港口”:5858,
“localRoot”:“${workspaceRoot}/dist/get时区”//