ded / reqwest

browser asynchronous http requests
2.94k stars 341 forks source link

context undefined when using webpack #259

Closed ondrejrohon closed 7 years ago

ondrejrohon commented 7 years ago

While using Auth0-js library that has reqwest as a dependency I get this error: Uncaught TypeError: Cannot use 'in' operator to search for 'window' in undefined.

I'm using webpack in my setup with following config:

const path = require('path');
const webpack = require('webpack');
const isProduction = process.argv.indexOf('-p') !== -1;

module.exports = {
  context: path.resolve(__dirname, './src'),
  devtool: 'source-map',
  entry: {
    app: './app.js',
  },
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, './dist/assets'),
    publicPath: '/assets',
  },

  devServer: {
    contentBase: path.resolve(__dirname, './dist'),
  },
  performance: {
    hints: false,
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        use: [{
          loader: 'babel-loader',
          options: { presets: ['react', 'es2015', 'babel-preset-stage-0'] }
        }],
      },
      {
        test: /\.(sass|scss)$/,
        use: ['style-loader', 'css-loader', 'sass-loader'],
      },
    ],
  },

  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': (isProduction) ? '"production"' : '""'
      }
    })
  ]

};

If I understand correctly, this on line 13 in reqwest.js is undefined because reqwest module is imported using webpack in a closure. Changing the line 13 in reqwest.js from var context = this to var context = this || window || {} fixes the issue. Do I have wrong configuration of webpack, or is reqwest module incorrectly defined? Thanks.

xurei commented 7 years ago

+1

seanpan commented 7 years ago

same issue

StefanoVuerich commented 7 years ago

+1

Bjornej commented 7 years ago

To anyone encountering this problem there's a workaround. I've had the same problem when upgrading to webpack 2 and it seems to be caused by an incorrect compilation of reqwest module. Changing

presets: ['es2015', 'react']

to

presets: [['es2015', {modules:false}], 'react']

disables module compilation and allows you to use the library

ondrejrohon commented 7 years ago

I can confirm that changing this settings in .babelrc as @Bjornej wrote works.

rodoabad commented 6 years ago

Has this issue been fixed without the workaround?

Bjornej commented 6 years ago

Sadly no, as reqwest seems to be no longer mantained and the last commit dates back to 2015

ondrejrohon commented 6 years ago

and it looks like auth0 does not have it as a dependency anymore