我正在尝试用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
和tsconfig.json
的每个组合,以便在TypeScript中获得相同的结果但无法获得任何效果。 如何让Webpack,TypeScript和React协同工作?
之前我使用过这三种技术,这是最近的兼容性问题吗?如果是这样,最新的兼容版本是什么?
我已经看到了一些与此类似的问题,其中解决方案是直接在项目中安装fbjs
- 我也尝试过这个问题但没有成功。
{
"compilerOptions": {
"target": "es5",
"jsx": "react",
"module": "es2015"
},
"exclude": ["build"]
}
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" },
],
},
};
{
...
"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"
}
}
import * as React from "react";
const a = <div />;
(我在安装了Node 9.2.1和NPM 5.6.0的情况下运行它)
答案 0 :(得分:16)
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",
"lib": ["es6", "dom"],
"sourceMap": true,
"moduleResolution": "node",
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true
},
"include": [
"./src"
],
"exclude": [
"node_modules",
"build"
]
}
答案 1 :(得分:-2)
在VSCode中,选中Deno Extension并将其设置为Enable(Workspace)