Webpack + React + TypeScript:模块未找到......在...... node_modules/react/
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47721962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Webpack + React + TypeScript: Module not found ... in ... node_modules/react/
提问by Sandy Gifford
I'm trying to put together a very basic project with React, TypeScript and Webpack. When I compile I get the following errors from the react
folder in node_modules
(I've removed the stack traces and my project's path for brevity):
我正在尝试将一个非常基本的项目与 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'
I tried uninstalling TypeScript and replacing it with Babel to transpile the JSX and got the same error. Installing babel-preset-2015
fixed it.
我尝试卸载 TypeScript 并用 Babel 替换它以转译 JSX 并得到相同的错误。 安装babel-preset-2015
固定它。
I've tried just about every combination of target
and module
in tsconfig.json
to get the same result in TypeScript but couldn't get anything working. How can I get Webpack, TypeScript, and React working together?
我已经尝试了几乎所有target
和module
in 的组合tsconfig.json
以在 TypeScript 中获得相同的结果,但无法获得任何工作。 如何让 Webpack、TypeScript 和 React 协同工作?
I've worked with all three of these technologies before, is it a recent compatibility problem? If so what are the most recent compatible versions?
我以前使用过所有这三种技术,这是最近的兼容性问题吗?如果是这样,最新的兼容版本是什么?
I've seen a few other questions similar to this one where the solution was installing fbjs
directly in the project - I've tried this too without success.
我已经看到了一些与此类似的其他问题,其中解决方案fbjs
直接安装在项目中 - 我也尝试过但没有成功。
Files
文件
tsconfig.json
配置文件
{
"compilerOptions": {
"target": "es5",
"jsx": "react",
"module": "es2015"
},
"exclude": ["build"]
}
webpack.config.js
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" },
],
},
};
package.json
包.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
./src/index.tsx
import * as React from "react";
const a = <div />;
(I'm running this with Node 9.2.1 and NPM 5.6.0 installed)
(我在安装了 Node 9.2.1 和 NPM 5.6.0 的情况下运行它)
回答by arpl
Webpack is not resolving .js
files. Add this to your webpack.config.js
.
Webpack 不解析.js
文件。将此添加到您的webpack.config.js
.
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx"]
},
Here is the tsconfig.json
I used to run your example.
这是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"
]
}