Webpack + React + TypeScript:module未找到......在...... node_modules/react/

IT技术 reactjs typescript webpack
2021-04-13 20:47:30

我正在尝试将一个非常基本的项目与 React、TypeScript 和 Webpack 放在一起。当我编译时,我从react文件夹中得到以下错误node_modules(为简洁起见,我删除了堆栈跟踪和我的项目路径):

ERROR in ./node_modules/react/cjs/react.production.min.js
Module not found: Error: Can't resolve 'fbjs/lib/emptyFunction' in '.../node_modules/react/cjs'

ERROR in ./node_modules/react/cjs/react.development.js
Module not found: Error: Can't resolve 'fbjs/lib/emptyFunction' in '.../node_modules/react/cjs'

ERROR in ./node_modules/react/cjs/react.production.min.js
Module not found: Error: Can't resolve 'fbjs/lib/emptyObject' in '.../node_modules/react/cjs'

ERROR in ./node_modules/react/cjs/react.development.js
Module not found: Error: Can't resolve 'fbjs/lib/emptyObject' in '.../node_modules/react/cjs'

ERROR in ./node_modules/react/cjs/react.development.js
Module not found: Error: Can't resolve 'fbjs/lib/invariant' in '.../node_modules/react/cjs'

ERROR in ./node_modules/react/cjs/react.development.js
Module not found: Error: Can't resolve 'fbjs/lib/warning' in '.../node_modules/react/cjs'

ERROR in ./node_modules/react/cjs/react.production.min.js
Module not found: Error: Can't resolve 'object-assign' in '.../node_modules/react/cjs'

ERROR in ./node_modules/react/cjs/react.development.js
Module not found: Error: Can't resolve 'object-assign' in '.../node_modules/react/cjs'

ERROR in ./node_modules/react/cjs/react.development.js
Module not found: Error: Can't resolve 'prop-types/checkPropTypes' in '.../node_modules/react/cjs'

我尝试卸载 TypeScript 并用 Babel 替换它以转译 JSX 并得到相同的错误。 安装babel-preset-2015固定它

我已经尝试了几乎所有targetmodulein 的组合tsconfig.json以在 TypeScript 中获得相同的结果,但无法获得任何工作。 如何让 Webpack、TypeScript 和 React 协同工作?

我以前使用过所有这三种技术,这是最近的兼容性问题吗?如果是这样,最新的兼容版本是什么?

我已经看到了一些与此类似的其他问题,其中解决方案fbjs直接安装在项目中 - 我也尝试过但没有成功。

文件

配置文件

{
  "compilerOptions": {
    "target": "es5",
    "jsx": "react",
    "module": "es2015"
  },
  "exclude": ["build"]
}

webpack.config.js

module.exports = {
    entry: {
        dev: "./src/index.tsx",
    },
    output: {
        filename: "./build/index.js",
    },
    devtool: "source-map",
    resolve: {
        extensions: [".ts", ".tsx"],
    },
    module: {
        loaders: [
            // Typescript
            { test: /\.tsx?$/, loader: "ts-loader" },
        ],
    },
};

包.json

{
    ...
    "scripts": {
        "build": "webpack"
    },
    "devDependencies": {
        "@types/react": "^16.0.28",
        "ts-loader": "^3.2.0",
        "typescript": "^2.6.2",
        "webpack": "^3.10.0"
    },
    "dependencies": {
        "react": "^16.2.0"
    }
}

./src/index.tsx

import * as React from "react";

const a = <div />;

(我在安装了 Node 9.2.1 和 NPM 5.6.0 的情况下运行它)

2个回答

Webpack 不解析.js文件。将此添加到您的webpack.config.js.

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

这是tsconfig.json我用来运行您的示例的。

{
  "compilerOptions": {
    "jsx": "react",
    "lib": ["es6", "dom"],
    "rootDir": "src",
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true,
    "moduleResolution": "node",
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true
  },
  "include": [
    "./src"
  ],
  "exclude": [
    "node_modules",
    "build"
  ]
}
做到了!对于遇到同样问题的人来说,最小的tsconfig也可以,仍然感谢您提供完整的设置!
2021-05-30 20:47:30

在 VSCode 中,选中 Deno Extension 并将其设置为 Enable(Workspace)

这应该是一个评论。
2021-06-03 20:47:30