我在将无服务器 v3 中的大型应用程序迁移到 v4 时遇到问题。我正在使用打字稿,并使用无服务器离线在本地进行开发。
但是在更改 v4 引入的所有配置并运行 sls offline start 后,所有启动速度都非常快,但是当我尝试访问一条路线时,出现此错误:
✖ Unhandled exception in handler 'externalUserInfo'.
[serverless-v2] ✖ Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/user/Development/aws/serverless-monorepo/node_modules/@middy/http-error-handler/package.json
[serverless-v2] at exportsNotFound (node:internal/modules/esm/resolve:304:10)
[serverless-v2] at packageExportsResolve (node:internal/modules/esm/resolve:594:13)
[serverless-v2] at resolveExports (node:internal/modules/cjs/loader:634:36)
[serverless-v2] at Function._findPath (node:internal/modules/cjs/loader:724:31)
[serverless-v2] at node:internal/modules/cjs/loader:1211:27
[serverless-v2] at nextResolveSimple (/Users/user/Development/aws/serverless-monorepo/node_modules/tsx/dist/register-DCnOAxY2.cjs:3:942)
[serverless-v2] at /Users/user/Development/aws/serverless-monorepo/node_modules/tsx/dist/register-DCnOAxY2.cjs:2:2550
[serverless-v2] at /Users/user/Development/aws/serverless-monorepo/node_modules/tsx/dist/register-DCnOAxY2.cjs:2:1624
[serverless-v2] at resolveTsPaths (/Users/user/Development/aws/serverless-monorepo/node_modules/tsx/dist/register-DCnOAxY2.cjs:3:760)
[serverless-v2] at Function._resolveFilename (/Users/user/Development/aws/serverless-monorepo/node_modules/tsx/dist/register-DCnOAxY2.cjs:3:1038)
无服务器(或无服务器离线)似乎完全忽略了我的处理程序位于打字稿中并且根本不编译的事实。我封锁了 3 个小时,没有任何解决方案...
这是我的 serverless.mjs 配置:
export default {
"frameworkVersion": "4",
"package": {
"individually": true,
"excludeDevDependencies": true
},
"provider": {
"name": "aws",
"region": "eu-west-1",
"runtime": "nodejs20.x",
"architecture": "arm64",
"memorySize": 1024,
"deploymentMethod": "direct",
"apiGateway": {
"minimumCompressionSize": 1024
},
"stage": "${opt:stage, \"local\"}"
},
"build": {
"esbuild": {
"external": [
"pg",
"pg-query-stream",
"better-sqlite3",
"tedious",
"mysql",
"oracledb",
"sqlite3",
"sharp"
],
"bundle": true,
"buildConcurrency": 1,
"exclude": [
"@aws-sdk"
],
"outputFileExtension": ".mjs",
"packagerOptions": {
"scripts": [
"npm install --os=linux --cpu=arm64 sharp"
]
}
}
},
"custom": {
"prune": {
"automatic": true,
"number": 3
},
"cors": {
"origin": "*",
"allowCredentials": false,
"headers": [
"Allow-Header",
"Content-Type",
"X-Amz-Date",
"Authorization",
"X-Api-Key",
"X-Api-Version",
"X-Amz-Security-Token",
"X-Amz-User-Agent",
"AuthToken",
"OrgId",
"CustomerId"
]
},
"serverless-offline": {
"noAuth": true,
"useChildProcesses": true,
"allowCache": true
}
},
"plugins": [
"serverless-offline"
],
"service": "serverless",
"functions": {
"externalUserInfo": {
"handler": "stacks/users/src/handlers/external-information-api.handler", // Typescript handler
"maximumRetryAttempts": 0,
"events": [
{
"http": {
"method": "get",
"path": "/account",
"private": true
}
}
]
},
}
}
目前,我找到的唯一解决方案并不完美:我停用无服务器构建过程
build.esbuild: false
并添加serverless-esbuild
插件。
之后,构建工作正常,但它不是一个完美的解决方案:正常情况下,使用 serverless-v4,我不必通过第三方模块来构建 typescript。