【问题标题】:cdk synth: Cannot find modulecdk 合成器:找不到模块
【发布时间】:2023-01-09 05:59:49
【问题描述】:

我正在为我的 CDK 包从相对路径导入切换到绝对路径导入,并在运行 cdk synth 时出现此错误:

$ cdk synth
Error: Cannot find module 'lib/CodePipelineStack'

我按照这个using absolute paths in typescript for importstsconfig.json 文件中设置了baseUrlpaths

不知道为什么它不工作。

更多上下文

我的项目结构如下所示:

我的 tsconfig.json 是:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "bin/*": [ "./bin/*" ],
      "lib/*": [ "./lib/*" ],
      "test/*": [ "./test/*" ]
    },
    "target": "ES2018",
    "module": "commonjs",
    "lib": [
      "es2018"
    ],
    "declaration": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": false,
    "inlineSourceMap": true,
    "inlineSources": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "typeRoots": [
      "./node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "cdk.out"
  ]
}

我试过 "baseUrl": "./""baseUrl": ".",都不起作用。

【问题讨论】:

    标签: typescript aws-cdk


    【解决方案1】:

    对于 Windows / 不起作用。您可以使用“sh ./..”或使用反斜杠

    【讨论】:

      【解决方案2】:

      cdk synth 在后台使用ts-nodets-node需要一个额外的包(tsconfig-paths)和一些额外的配置来根据tsconfig.json中的paths配置加载模块:

      {
        "ts-node": {
          "require": ["tsconfig-paths/register"]
        }
      }
      

      进一步的文件: https://github.com/TypeStrong/ts-node#paths-and-baseurl

      你的最终 tsconfig.json 看起来像这样:

      {
        "compilerOptions": {
          "baseUrl": ".",
          "paths": {
            "bin/*": [ "./bin/*" ],
            "lib/*": [ "./lib/*" ],
            "test/*": [ "./test/*" ]
          },
          "target": "ES2018",
          "module": "commonjs",
          "lib": [
            "es2018"
          ],
          "declaration": true,
          "strict": true,
          "noImplicitAny": true,
          "strictNullChecks": true,
          "noImplicitThis": true,
          "alwaysStrict": true,
          "noUnusedLocals": false,
          "noUnusedParameters": false,
          "noImplicitReturns": true,
          "noFallthroughCasesInSwitch": false,
          "inlineSourceMap": true,
          "inlineSources": true,
          "experimentalDecorators": true,
          "strictPropertyInitialization": false,
          "typeRoots": [
            "./node_modules/@types"
          ]
        },
        "exclude": [
          "node_modules",
          "cdk.out"
        ], 
        "ts-node": {
          "require": ["tsconfig-paths/register"]
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-18
        • 2016-12-07
        • 1970-01-01
        • 1970-01-01
        • 2015-05-14
        • 2022-12-15
        • 2013-01-06
        相关资源
        最近更新 更多