我正在尝试将一个非常基本的项目与 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
固定它。
我已经尝试了几乎所有target
和module
in 的组合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 的情况下运行它)