[英]Karma, Webpack & Typescript: TS2307: Cannot find module 'log4js' or its corresponding type declarations
When running karma ( npx karma start
) I get an error about a module运行 karma (
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
It does succeed executing the tests, but it throws an error about log4js.它确实成功执行了测试,但它引发了关于 log4js 的错误。 How can I solve this error?
我该如何解决这个错误? (I already tried installing log4js npm package)
(我已经尝试安装 log4js npm 包)
Karma config:业力配置:
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 config (simplified but I confirmed this throws the same error): 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"]
}
What I already tried:我已经尝试过的:
Installing log4js by running yarn add log4js
and yarn add @types/log4js
通过运行
yarn add log4js
和yarn add @types/log4js
For now I have a solution, but if anyone knows a better solution I'd love to know.现在我有一个解决方案,但如果有人知道更好的解决方案,我很想知道。 Because this requires copying a file from node_modules, and it won't be updated if the package is updated.
因为这需要从 node_modules 复制一个文件,并且如果 package 更新,它将不会更新。
I copied the contents from node_modules/log4js/types/log4js.d.ts
to a new file I made at @types/log4js/index.dts
(Note this is our own @types directory as configured in our tsconfig seen above, it's not the folder in node modules).我将
node_modules/log4js/types/log4js.d.ts
中的内容复制到我在@types/log4js/index.dts
创建的一个新文件中(注意这是我们在上面的 tsconfig 中配置的我们自己的@types 目录,它不是节点模块中的文件夹)。 Then I slightly modified this index by adding this at the top of the file:然后我通过在文件顶部添加这个来稍微修改这个索引:
declare module "log4js" {
And at the bottom of the file:在文件的底部:
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.