【问题标题】:Could not locate module ... mapped as for one of my packges找不到模块...映射为我的一个包
【发布时间】:2021-08-13 12:23:18
【问题描述】:

我有一个my-dependency-package,这是我正在使用的 NPM 包,其中包括 lib/utils/list-utils

我有my-package,它使用my-dependency-package 作为依赖项。当我尝试构建它时,我收到此错误:

FAIL  test/some-file.test.ts
  ● Test suite failed to run
    Configuration error:
    Could not locate module lib/utils/list-utils mapped as:
    /some-path/my-package/lib/utils/list-utils.
    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^lib\/(.*)$/": "/some-path/my-package/lib/$1"
      },
      "resolver": null
    }
      at createNoMappedModuleFoundError (node_modules/jest-resolve/build/index.js:501:17)
      at Object.<anonymous> (node_modules/@amzn/quicksilver-commons-cdk/lib/pipelines/quicksilver-pipeline.ts:4:1)
 FAIL  test/stack/order-aggregation-goldbox-lambda-stack.test.ts
  ● Test suite failed to run
    Configuration error:
    Could not locate module lib/utils/list-utils mapped as:
    /some-path/my-package/lib/utils/list-utils.
    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^lib\/(.*)$/": "/some-path/my-package/lib/$1"
      },
      "resolver": null
    }

这是package.jsonmy-package

{
  "name": "my-package",
  "version": "1.0.0",
  "scripts": {
    "clean": "rm -rf ./build && rm -rf ./dist && rm -rf node_modules",
    "test": "npm run lint && jest --passWithNoTests",
    "posttest": "generate-coverage-data",
    "prettier": "prettier bin/**/*.ts lib/*.ts lib/**/*.ts --write",
    "lint": "eslint '**/*.{ts,js}'"
  },
  "dependencies": {
    // some dependencies
    "my-dependency-package": "*"
  },
  "devDependencies": {
    // some devDependencies
  },
  "_moduleAliases": {
    "lib": "lib"
  }
}

最后是tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2018",
    "module": "commonjs",
    "lib": ["es2016", "es2017.object", "es2017.string"],
    "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"],
    "baseUrl": "./",
    "paths": {
        "lib/*": ["lib/*"],
        "test/*": ["test/*"],
      }
  },
  "exclude": ["cdk.out"]
}

如果需要,我也可以为my-dependency-package分享相同的文件。

【问题讨论】:

    标签: javascript node.js typescript npm


    【解决方案1】:

    查看错误堆栈跟踪,我会说 jest 配置错误。

    ● Test suite failed to run
        Configuration error:
        Could not locate module lib/utils/list-utils mapped as:
        /some-path/my-package/lib/utils/list-utils.
        Please check your configuration for these entries:
        {
          "moduleNameMapper": {
            "/^lib\/(.*)$/": "/some-path/my-package/lib/$1"
          },
          "resolver": null
        }
    

    您可能想要创建一个jest.config.ts 文件并使用--config 标志将其添加到您的测试命令的参数中。

    https://jestjs.io/docs/configuration#modulenamemapper-objectstring-string--arraystring

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2018-02-16
      • 2019-12-09
      • 2020-12-13
      • 2021-05-02
      • 2017-05-06
      • 2016-09-02
      相关资源
      最近更新 更多