业力,Webpack和打字稿:TS2307:找不到模块“ log4js”或其相应的类型声明

时间:2020-07-23 11:14:38

标签: typescript webpack karma-runner karma-mocha karma-webpack

运行业力(npx karma start)时,我收到有关模块的错误

× 「atl」: Checking finished with 1 errors
i 「atl」: Checking started in a separate process...
× 「atl」: Checking finished with 1 errors
× 「wdm」:
ERROR in [at-loader] ./node_modules/@types/karma/index.d.ts:14:26
    TS2307: Cannot find module 'log4js' or its corresponding type declarations.
i 「wdm」: Failed to compile.
23 07 2020 12:53:39.790:WARN [karma]: No captured browser, open http://localhost:9876/
23 07 2020 12:53:39.803:INFO [karma-server]: Karma v5.1.0 server started at http://0.0.0.0:9876/
23 07 2020 12:53:39.803:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
23 07 2020 12:53:39.820:INFO [launcher]: Starting browser Chrome
23 07 2020 12:53:41.358:INFO [Chrome 84.0.4147.89 (Windows 10)]: Connected on socket CIrhkw1m_-3oa6JsAAAA with id 44315598
Chrome 84.0.4147.89 (Windows 10): Executed 0 of 1 SUCCESS (0 secs / 0 secs)
Chrome 84.0.4147.89 (Windows 10): Executed 1 of 1 SUCCESS (0.125 secs / 0.002 secs)
TOTAL: 1 SUCCESS

它确实成功执行了测试,但是引发了有关log4js的错误。我该如何解决这个错误? (我已经尝试安装log4js npm软件包)

业力配置:

    config.set({
        basePath: "",
        frameworks: ["mocha"],
        files: [
            "UnitTests/**/*.browsertest.ts",
        ],
        preprocessors: {
            "UnitTests/**/*.browsertest.ts": "webpack",
        },
        webpack: webpackConfig,
        webpackMiddleware: {
          stats: 'errors-only'
        },
        reporters: ["progress"],
        browsers: ["Chrome"],
        autowatch: false,
    });

Webpack配置(简化,但我确认这会引发相同的错误):

{
      mode: "development",
      entry: {
         "dist/main.bundle": "./TypeScript/Main.entry.ts",
      },
      output: {
         path: path.join(__dirname, "wwwroot"),
         filename: "[name].js"
      },
      devtool: "source-map",
      resolve: {
         extensions: [".ts", ".js"]
      },
      module: {
         rules: [
            { 
               test: /\.tsx?$/, exclude: [/node_modules/], 
               use: ["awesome-typescript-loader"]
            }
         ]
      }
   }

tsconfig:

{
   "compileOnSave": true,
   "compilerOptions": {
      "experimentalDecorators": true,
      "sourceMap": true,
      "strictNullChecks": true,
      "noImplicitAny": true,
      "module": "amd",
      "target": "es6",
      "jsx": "react",
      "jsxFactory": "renderDom",
      "declaration": false,
      "esModuleInterop": true,
      "lib": [
         "WebWorker",
         "DOM",
         "es6"
      ],
      "typeRoots": [
         "./node_modules/@types",
         "./@types"
      ]
   },
   "include": [
      "TypeScript/**/*",
      "UnitTests/**/*"
   ],
   "exclude": ["node_modules"]
}

我已经尝试过的方法:

通过运行yarn add log4jsyarn add @types/log4js安装log4js

1 个答案:

答案 0 :(得分:0)

现在我有一个解决方案,但是如果有人知道更好的解决方案,我很想知道。因为这需要从node_modules复制一个文件,并且如果软件包被更新,它也不会被更新。

我将内容从node_modules/log4js/types/log4js.d.ts复制到了我在@types/log4js/index.d.t.s创建的新文件中 (请注意,这是我们自己的@types目录,如上面在tsconfig中配置的那样,它不是节点模块中的文件夹)。 然后,我通过在文件顶部添加该索引来对其稍加修改:

declare module "log4js" {

在文件底部:

}