【发布时间】:2016-10-13 06:50:59
【问题描述】:
我正在尝试使用 Typescript 和 React 以及 webpack 制作一个简单的演示应用程序。当我尝试添加 whatgw-fetch 以从服务器检索数据时,运行 webpack 时出现此错误:
error TS2307: Cannot find module 'whatwg-fetch'
错误在导入行:
import * as Fetch from 'whatwg-fetch'
我确实安装了 npm 依赖项和 typescript 的输入
npm install --save whatwg-fetch
typings install --global --save dt~whatwg-fetch
我的 webpack 配置是:
var HtmlWebpackPlugin = require('html-webpack-plugin');
var HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
template: __dirname + '/app/index.html',
filename: 'index.html',
inject: 'body'
});
var path = require('path');
module.exports = {
entry: [
path.resolve(__dirname, 'app/index.tsx')
],
output: {
path: __dirname + '/dist',
filename: "index_bundle.js"
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js']
},
module: {
loaders: [
{test: /\.tsx$/, exclude: /node_modules/, loader: "ts-loader"}
]
},
plugins: [HTMLWebpackPluginConfig]
};
我在我的 IDE (IntelliJ IDEA) 中没有看到任何错误,如果我将导入更改为某个实际上不存在的模块,我会收到另一个错误 (Module not found: Error: Cannot resolve module 'whatwg-fetcha' in C:\dev\[...]),并且我的 IDE 会告诉我导入无效。
基本 React 的导入可以在等效设置下正常工作:
npm install --save react
typings install --global --save dt~react
import * as React from 'react'
我错过了什么吗?
感谢您的帮助。
【问题讨论】:
标签: reactjs typescript webpack fetch