Sto provando ad avere una semplice app demo che funziona con Typescript e React, con webpack. Quando provo ad aggiungere whatgw-fetch per recuperare i dati da un server, ottengo questo errore durante l'esecuzione webpack:Errore con Typescript/whatwg-fetch/webpack
error TS2307: Cannot find module 'whatwg-fetch'
L'errore si trova sulla linea di importazione:
import * as Fetch from 'whatwg-fetch'
ho installato il NPM dipendenza e la tipizzazione per dattiloscritto
npm install --save whatwg-fetch
typings install --global --save dt~whatwg-fetch
la mia configurazione è 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]
};
Non vedo alcun errore nel mio IDE (IntelliJ IDEA) e se cambio l'importazione in qualche modulo che in realtà non c'è, ottengo un errore diverso (Module not found: Error: Cannot resolve module 'whatwg-fetcha' in C:\dev\[...]
) e il mio IDE mi dice che l'importazione è non valido.
L'importazione per Reagire opere fondamentali fine con un ambiente equivalente:
npm install --save react
typings install --global --save dt~react
import * as React from 'react'
Mi sto perdendo qualcosa?
Grazie per il vostro aiuto.
ciao, ho provato lo stesso, ma dice a prendermi non è definito in fase di esecuzione. qualche soluzione? –
è necessario utilizzare lo shim github per il polyfill per i browser che non lo supportano ancora: https://github.com/github/fetch @SalmanHasratKhan – BentOnCoding
Semplicemente stai usando typescript e ottieni "Promise non è definito "errori, assicurati che il tuo target nel file tsconfig sia" es6 "o superiore. – user31208