如何解决TypeScript错误仅出现在第一个Webpack编译中
我正在学习TypeScript,设置它给我带来了困难。
如果添加语法错误或其他内容,则启动webpack-dev-server
,编译将失败,但是如果保存并获取服务器进行重新编译,该错误将消失。开发服务器已在运行时添加的错误也不会发生。看来
webpack-dev-server
初始输出
yarn run v1.22.10
$ npx webpack-dev-server --config goal_tracker/webpack/dev.config.js
ℹ 「wds」: Project is running at http://localhost:3000/
ℹ 「wds」: webpack output is served from /static/
ℹ 「wds」: Content not from webpack is served from /home/sean/sourcecode/goal_tracker
✖ 「wdm」: asset main.js 3.69 MiB [emitted] (name: main)
runtime modules 24.8 KiB 13 modules
modules by path ./node_modules/ 1.35 MiB 70 modules
modules by path ./client/ 8.15 KiB
modules by path ./client/apps/common/ 2.99 KiB
modules by path ./client/apps/common/*.ts 415 bytes 3 modules
modules by path ./client/apps/common/hooks/*.ts 2.58 KiB 3 modules
modules by path ./client/apps/tests/ 5.13 KiB
modules by path ./client/apps/tests/styles/*.scss 2.1 KiB
./client/apps/tests/styles/main.scss 1.44 KiB [built] [code generated]
./node_modules/css-loader/dist/cjs.js!./node_modules/sass-loader/dist/cjs.js!./client/apps/tests/styles/main.scss 680 bytes [built] [code generated]
./client/apps/tests/index.tsx 1.28 KiB [built] [code generated]
./client/apps/tests/users/register.tsx 1.75 KiB [built] [code generated]
./client/packs/tests.ts 38 bytes [built] [code generated] [1 error]
ERROR in /home/sean/sourcecode/goal_tracker/client/packs/tests.ts
./client/packs/tests.ts
[tsl] ERROR in /home/sean/sourcecode/goal_tracker/client/packs/tests.ts(2,1)
TS2304: Cannot find name 'isNotDefined'.
webpack 5.3.2 compiled with 1 error in 4725 ms
ℹ 「wdm」: Failed to compile.
ℹ 「wdm」: Compiling...
ℹ 「wdm」: asset main.js 3.69 MiB [emitted] (name: main)
runtime modules 24.8 KiB 13 modules
modules by path ./node_modules/ 1.35 MiB 70 modules
modules by path ./client/ 8.15 KiB
modules by path ./client/apps/common/ 2.99 KiB
modules by path ./client/apps/common/*.ts 415 bytes 3 modules
modules by path ./client/apps/common/hooks/*.ts 2.58 KiB 3 modules
modules by path ./client/apps/tests/ 5.13 KiB
modules by path ./client/apps/tests/styles/*.scss 2.1 KiB
./client/apps/tests/styles/main.scss 1.44 KiB [built] [code generated]
./node_modules/css-loader/dist/cjs.js!./node_modules/sass-loader/dist/cjs.js!./client/apps/tests/styles/main.scss 680 bytes [built] [code generated]
./client/apps/tests/index.tsx 1.28 KiB [built] [code generated]
./client/apps/tests/users/register.tsx 1.75 KiB [built] [code generated]
./client/packs/tests.ts 38 bytes [built] [code generated]
webpack 5.3.2 compiled successfully in 1977 ms
ℹ 「wdm」: Compiled successfully.
我的webpack配置
const path = require("path")
module.exports = {
mode: "development",bail: true,cache: false,entry: "./client/packs/tests.ts",output: {
path: path.resolve("./client/static/client/js"),filename: "[name].js",chunkFilename: "[id]-[chunkhash].js",publicPath: "/static/",},devServer: {
overlay: true,port: 3000,hot: true,compress: true,headers: {
"Access-Control-Allow-Origin": "*",module: {
rules: [
{
test: /\.tsx?$/,use: "ts-loader",exclude: /node_modules/,{
test: /\.s[ac]ss$/i,use: [
"style-loader","css-loader","sass-loader",],resolve: {
extensions: [".ts",".tsx",".js"],devtool: "inline-source-map",};
我的tsconfig
{
"compilerOptions": {
"module": "es6","target": "es5","noImplicitAny": true,"removeComments": true,"outDir": "./client/static/client/js/","sourceMap": true,"jsx": "react","allowJs": true,"allowSyntheticDefaultImports": true,"moduleResolution": "node","strict": true,"strictPropertyInitialization": false
},"exclude": ["**/*.spec.ts","node_modules","vendor","public"],"compileOnSave": false
}
提前感谢您的帮助!
解决方法
我修复了!
我发现我使用的是全球安装的webpack-dev-server版本,由于某种原因导致了该问题,因此我将其添加到package.json
中并进行了安装。现在工作正常
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。