Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation #46
Comments
|
Ah interesting, the |
|
You mean webpack setup? |
|
I have the same issue using Webpack 3 and Babel (babel-preset-env). Original code: import fetch from 'unfetch';
const test = fetch('http://localhost:8080');Compiled code: /* harmony import */ var __WEBPACK_IMPORTED_MODULE_2_unfetch__ = __webpack_require__(420);
const test = __WEBPACK_IMPORTED_MODULE_2_unfetch__["a" /* default */]('http://localhost:8080');Note how Fails on Chrome with
and on Firefox with
Interestingly, Safari doesn't care. The following workaround works well. import unfetch from 'unfetch';
// Fix a bug making unfetch not being properly bound with webpack.
const fetch = unfetch;
const test = fetch('http://localhost:8080');A simple fix for export default typeof fetch=='function' ? fetch : function(url, options) {
// [...]
}with something like export default typeof fetch=='function' ? fetch.bind() : function(url, options) {
// [...]
}If it is of any use, here is my webpack config. const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const resolve = relPath => path.resolve(__dirname, relPath);
module.exports = {
entry: {
experiment: ['babel-polyfill', './experiment'],
'results-viewer': ['babel-polyfill', './results-viewer']
},
output: {
path: resolve('build'),
publicPath: '/',
filename: './[name].js'
},
context: resolve('src'),
resolve: {
extensions: ['.js', '.json', '.jsx', '.css'],
alias: {
'state-machine': 'javascript-state-machine'
}
},
module: {
rules: [{
test: /\.pug/,
include: resolve('src'),
use: 'pug-loader'
}, {
test: /\.s?css$/,
include: resolve('src'),
use: ExtractTextPlugin.extract({
use: [{
loader: 'css-loader',
options: { sourceMap: true }
}, {
loader: 'postcss-loader',
options: { sourceMap: true }
}, {
loader: 'sass-loader',
options: { sourceMap: true }
}]
})
}, {
test: /\.css$/,
exclude: resolve('src'),
use: ExtractTextPlugin.extract({
use: [{
loader: 'css-loader',
options: { sourceMap: true }
}]
})
}, {
test: /\.m?js$/,
include: resolve('src'),
use: 'babel-loader'
}]
},
plugins: [
new ExtractTextPlugin({ filename: '[name].css', disable: false, allChunks: true }),
]
};and my .babelrc {
"presets": [["env", {
"targets": { "browsers": "last 1 chrome version, last 1 safari version, last 1 firefox version" },
"modules": false,
"loose": true
}]],
"plugins": ["transform-runtime"]
} |
Fix fetch errors.
|
Seems like this is still a thing. I'm using:
On server side it's working as expected, but when I try something like making a call inside a simple |
The script crashed with error
TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
After webkack it is looks: