serverless framework not recognizing tsconfig's paths
我正在尝试使用无服务器框架将Node.js应用程序部署到Lambda,但是,我的节点代码使用tsconfig.json中的路径来引用导入,但是无服务器部署过程失败。如何连接serverless.yml以确认并使用tsconfig.json中定义的路径?
我已经安装了serverless-plugin-typescript,但是它似乎无法识别tsconfig中的路径。
serverless.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 | service: name: app-name custom: webpack: webpackConfig: ./webpack.config.js includeModules: true plugins: - serverless-webpack - serverless-plugin-typescript ... |
tsconfig.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | { "compileOnSave": true, "compilerOptions": { "lib": [ "es2017" ], "baseUrl":"./src", "declaration": true, "downlevelIteration": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "module":"commonjs", "moduleResolution":"node", "noUnusedLocals": true, "noUnusedParameters": true, "outDir":"./dist", "paths": { "@config/*": [ "config/*" ], "@core/*": [ "core/*" ], "@infra/*": [ "infra/*" ], "@modules/*": [ "modules/*" ], "@shared/*": [ "shared/*" ] }, "preserveConstEnums": true, "removeComments": true, "rootDir":"./src", "skipLibCheck": true, "sourceMap": true, "strict": true, "strictNullChecks": false, "target":"es2017", "typeRoots": [ "node_modules/@types" ], "types": [ "node" ] }, "include": [ "./**/*.ts" ], "exclude": [ "node_modules/**/*", ".serverless/**/*", ".webpack/**/*", "_warmup/**/*", ".vscode/**/*" ] } |
我找到了答案。原来您需要安装tsconfig-paths-webpack-plugin。然后在webpack.config.js中添加以下内容:
1 | npm install --save-dev tsconfig-paths-webpack-plugin |
内部webpack.config.js:
1 2 3 4 5 6 7 8 9 10 | const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'); module.exports = { ... resolve: { plugins: [new TsconfigPathsPlugin({ configFile:"./tsconfig.json" })] }, ... }; |
注意:请确保使用resolve部分中的插件。根目录有一个插件,但是TsconfigPathsPlugin仅适用于resolve / plugins。
如果遇到同样的问题,希望对您有所帮助。