我正在使用无服务器框架为我的 Web 应用程序创建节点 API 后端。 VSCode 是我的 IDE,在 Windows 11 上运行。
VSCODE 运行和调试小部件立即失败,并出现我无法理解或修复的错误。
这是我的launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "run SLS",
"program": "${workspaceRoot}/api/fq-transcribe/node_modules/.bin/sls",
"args": ["invoke", "local", "-f", "fqTranscribe", "-p", "./test-events/event.json"]
}
]
}
这是我得到的错误:
Uncaught SyntaxError C:\Users\david\build\frequency\api\fq-transcribe\node_modules\.bin\sls:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
at compileFunction (vm:360:18)
at wrapSafe (internal/modules/cjs/loader:1084:15)
at Module._compile (internal/modules/cjs/loader:1119:27)
at Module._extensions..js (internal/modules/cjs/loader:1209:10)
at Module.load (internal/modules/cjs/loader:1033:32)
at Module._load (internal/modules/cjs/loader:868:12)
at executeUserEntryPoint (internal/modules/run_main:81:12)
at <anonymous> (internal/main/run_main_module:22:47)
vm:360
Process exited with code 1
这是 SLS 文件:
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/../serverless/bin/serverless.js" "$@"
else
exec node "$basedir/../serverless/bin/serverless.js" "$@"
fi
在同事的帮助下,我成功了。我现在可以在 Serverless Offline 上以及在 Windows 上使用 Serverless Invoke Local 进行断点调试。我们还知道如何使用 Mac 和 Linux,我也会分享这一点。
这是
launch.json
文件。第一个配置用于 Serverless Offline,第二个配置用于 Invoke Local。您可能需要更新 cwd 才能到达包含 serverless.yml 文件的目录。
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Serverless Offline",
"cwd": "${workspaceFolder}",
"skipFiles": [
"<node_internals>/**"
],
"args": [
"run",
"debug"
],
"sourceMaps": true,
"runtimeExecutable": "npm"
},
{
"type": "node",
"request": "launch",
"name": "Debug Serverless Invoker",
"cwd": "${workspaceFolder}",
"skipFiles": [
"<node_internals>/**"
],
"args": [
"run",
"invoker"
],
"sourceMaps": true,
"runtimeExecutable": "npm"
}
]
}
将其添加到您的 package.json 中:
"scripts": {
"debug": "node --inspect node_modules/serverless/bin/serverless offline -s dev --reloadHandler",
"invoker": "node --inspect node_modules/serverless/bin/serverless invoke local -f YourFunctionNameHere"
},
对于 Mac/Linux,除了一件事之外,它是完全相同的: 在 package.json 中:runtimeExecutable 必须是 npm 的完整路径。使用
which npm
获取值。
作为 @dcyprianrw 的后续工作,请确保 tsconfig 的
outDir
与 outDir
使用的 serverless-plugin-typescript
相匹配。看起来 vscode 调试器将在 tsconfig.json 的 outDir
中搜索源映射(即使您使用 outFiles
中的 launch.json
将其指向正确的方向)
来自 serverless-plugin-typescript 文档:
// The default tsconfig.json file used by the plugin looks like this:
{
"compilerOptions": {
"preserveConstEnums": true,
"strictNullChecks": true,
"sourceMap": true,
"allowJs": true,
"target": "es5",
"outDir": ".build",
"moduleResolution": "node",
"lib": ["es2015"],
"rootDir": "./"
}
}
如果您想使用自定义 tsconfig,则必须在 serverless-local.yml 中指定路径:
custom:
serverlessPluginTypescript:
tsConfigFileLocation: './tsconfig.build.json'