首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不识别tsconfig路径的无服务器框架

不识别tsconfig路径的无服务器框架
EN

Stack Overflow用户
提问于 2020-05-21 14:35:00
回答 1查看 2.6K关注 0票数 4

我试图使用Serverless框架将我的Node.js应用程序部署到Lambda,但是,我的节点代码使用来自tsconfig.json的路径来引用导入,但是Serverless部署过程失败了。如何连接serverless.yml以确认和使用在tsconfig.json中定义的路径?

我已经安装了无服务器插件类型记录,但它似乎无法从tsconfig中识别路径。

serverless.yml

代码语言:javascript
复制
service:
  name: app-name

custom:
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true

plugins:
  - serverless-webpack
  - serverless-plugin-typescript

...

tsconfig.ts

代码语言:javascript
复制
{
  "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/**/*"
  ]
}
EN

回答 1

Stack Overflow用户

发布于 2020-05-21 15:33:29

我找到了答案。结果发现,您需要安装tsconfig-路径-webpack-插件。然后在webpack.config.js中添加以下内容:

代码语言:javascript
复制
npm install --save-dev tsconfig-paths-webpack-plugin

在webpack.config.js内部:

代码语言:javascript
复制
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');


module.exports = {
  ...
  resolve: {
    plugins: [new TsconfigPathsPlugin({ configFile: "./tsconfig.json" })]
  },
  ...
};

注意:一定要使用解决部分中的插件。有一个插件是根,但是TsconfigPathsPlugin只在解析/插件中工作。

如果你经历了同样的问题,我希望这对你有帮助。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61936928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档