使用react typescript webpack报错总结

报错

ERROR in [at-loader]
 ../wechat_web_devtools/packages/node-sync-ipc/index.d.ts:1:32 
TS2307: Cannot find module 'child_process'.

缺少 tslint.json tsconfig.json文件

报错

WARNING in configuration
The 'mode' option has not been set, 
webpack will fallback to 'production' for this value. 
Set 'mode' option to 'development' or 'production' to enable defaults 
for each environment.
You can also set it to 'none' to disable any default behavior. 
Learn more: https://webpack.js.org/concepts/mode/

webpack.config.js 没有设置mode选项
可以是production 或者 development

报错

ERROR in ./lib/index.tsx
Module not found: Error: 
Can't resolve './button' in '.../react-gulu-demo/lib'
 @ ./lib/index.tsx 3:0-30 4:36-42

webpack.config.js需要配置 resolve

    resolve:{
      extensions:['.ts','.tsx','.js','.jsx']
    }

报错

ERROR in [at-loader] ./lib/button.tsx:4:10 
TS2686: 'React' refers to a UMD global,
 but the current file is a module. Consider adding an import instead.

组件文件 需要引入react

报错

WARNING in asset size limit: The following asset(s) exceed the recommended
size limit (244 KiB).
This can impact web performance.
Assets: 
  index.js (260 KiB)
WARNING in entrypoint size limit: 
The following entrypoint(s) combined asset size exceeds 
the recommended limit (244 KiB). 
This can impact web performance.
Entrypoints:
  index (260 KiB)
      index.js
WARNING in webpack performance recommendations: 
You can limit the size of your bundles by using import() or require.
ensure to lazy load some parts of your application.
For more info visit https://webpack.js.org/guides/code-splitting/

资源超过了推荐的大小
可以将mode 设置成 development
或者设置 extennals

externals: {
        react: {
            commonjs: "react",
            commonjs2: "react",
            amd: "react",
            root: "React",
        },
        "react-dom": {
            commonjs: "react-dom",
            commonjs2: "react-dom",
            amd: "react-dom",
            root: "ReactDOM",
        },
    }

报错

Warning: render(): Rendering components directly into document.body i
s discouraged, 
since its children are often manipulated by third-party scripts 
and browser extensions. 
This may lead to subtle reconciliation issues. 
Try rendering into a container element created for your app.

不能直接将组件render到body上

报错

Validation Error:
Module ts-jest in the transform option was not found.
 is: xxx/xxx
Configuration Documentation:
https://jestjs.io/docs/configuration.html

没找到 ts-jest 需要安装

报错

● Validation Error:
Module test/setupTests.js in the setupFilesAfterEnv option 
was not found.
 is: xxx/xxx
Configuration Documentation:
https://jestjs.io/docs/configuration.html

找不到setupTests.js文件

报错

Deprecation Warning:
Option "setupTestFrameworkScriptFile" was replaced by configuration 
"setupFilesAfterEnv", which supports multiple paths.
Please update your configuration.
Configuration Documentation:
https://jestjs.io/docs/configuration.html

setupTestFrameworkScriptFile 弃用了 改setupFilesAfterEnv了

报错

error TS7016: 
Could not find a declaration file for module 'react-test-renderer'. 
'xxx/node_modules/react-test-renderer/index.js'
 implicitly has an 'any' type.
      Try `npm install @types/react-test-renderer`
 if it exists or 
add a new declaration (.d.ts) file 
containing `declare module 'react-testrenderer';`

不能找到react-test-renderer类型声明文件

报错

TypeError: Cannot read property 'create' of undefined
      1 | import renderer from 'react-test-renderer'
      2 | import React from "react"
      3 | import Button from "../button";
      4 | describe("button", function () {
      5 |     it("是一个div",()=>{
    > 6 |         const json = renderer.create(

测试文件 renderer不存在?
react-test-renderer没有默认导出

 import * as renderer from 'react-test-renderer'
 import * as React from "react"

报错

Jest encountered an unexpected token
This usually means that you are trying to import a file 
which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform
 your files, 
ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed,
you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option 
in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) 
you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/xxx/lib/icon/icon.scss:1
({"Object.":
function(module,exports,require,__dirname,__filename,global,jest)
{.xrui-icon{width:1em;height:1em}}
    > 3 | import "./icon.scss";
        | ^

jest 不能识别scss 需要配置 jest.config.js moduleNameMapper

报错

Default export is not declared in imported module

需要 import * as xxx from "xxx"

报错

Cannot find name 'Set'. 
Do you need to change your target library? 
Try changing the `lib` compiler option to es2015 or later.

Cannot find name 'document'. 
Do you need to change your target library? 
Try changing the `lib` compiler option to include 'dom'.

你需要在tsconfig.json 中配置lib option

{
  "compilerOptions": {
    ...,
    "lib": [ "es6", "dom" ],
  }
}

报错

 TS2322: Type '{ children: string; onclick: () => void; }' 
is not assignable to type 
'DetailedHTMLProps, 
HTMLButtonElement>'.
  Property 'onclick' does not exist on type 
'DetailedHTMLProps, 
HTMLButtonElement>'.

手贱 改为onClick

报错

Cannot invoke an expression whose type lacks a call signature. 
Type 'void' has no compatible call signatures.

调用的表达式 返回值是 void 手动添加return xxx

报错

TS1016: A required parameter cannot follow an optional parameter.

必需参数不能跟随可选参数 两个参数可以换个位置

你可能感兴趣的:(使用react typescript webpack报错总结)