AWS Serverless Node.js16应用sls offline运行时esbuild装饰器转换报错
解决AWS Serverless Node.js v16.20中sls offline的esbuild装饰器转换错误
问题描述
在AWS Serverless Node.js(v16.20)项目中执行命令 sls offline -s local 时,esbuild抛出如下错误:
Transforming JavaScript decorators to the configured target environment ("node16") is not supported yet
当前使用的tsconfig配置如下:
{ "compilerOptions": { "moduleResolution": "Node", "alwaysStrict": true, "declaration": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "noImplicitAny": true, "preserveConstEnums": true, "sourceMap": true, "resolveJsonModule": true, "noUnusedLocals": true, "noImplicitThis": true, "strictNullChecks": true, "noImplicitReturns": true, "suppressImplicitAnyIndexErrors": true, "forceConsistentCasingInFileNames": true, "strictPropertyInitialization": false, "outDir": "dist", "rootDirs": ["src"] }, "exclude": [ "node_modules", ".webpack", ".serverless", ".esbuild" ] }
解决方案
方案1:配置esbuild使用旧版装饰器语法
在serverless.yml中添加esbuild自定义配置,指定适配node16的旧版装饰器转换逻辑:
custom: esbuild: decorators: "legacy" target: "node16"
方案2:明确tsconfig的编译目标版本
在tsconfig的compilerOptions中补充target字段,设置为与node16兼容的ES版本(如ES2021),确保TypeScript编译输出与esbuild目标环境匹配:
{ "compilerOptions": { // 保留原有配置 "target": "ES2021" } }
方案3:升级Node.js版本(备选)
若项目允许,将Node.js升级至v18及以上版本,新版Node对装饰器语法的原生支持更完善,esbuild可直接适配对应环境的转换需求。
内容的提问来源于stack exchange,提问作者Harpreet Singh
相关产品推荐
相关产品推荐

