Babel-loader: global is not defined with Babel 6

Created on 17 Jan 2016  Â·  9Comments  Â·  Source: babel/babel-loader

Since upgrading to babel 6, my build compiles but errors in the browser:

Uncaught ReferenceError: global is not defined
(anonymous function)    @   SetCache.js?9507:5

I've followed the babel 6 upgrade guide, and things seem to be working out - until running in the browser.

.babelrc

{
  "presets": [
    "es2015",
    "stage-0",
    "react"
  ]
}

webpack config

var path = require('path')
var webpack = require('webpack')

var config = {
  devtool: 'cheap-module-eval-source-map',
  target: 'node',
  entry: [
    'webpack-hot-middleware/client',
    './client/index'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new webpack.DefinePlugin(
      {
        'process.env.NODE_ENV': '"development"',
        'global.GENTLY': false
      }
    ),
  ],
  module: {
    loaders: [
      { test: /\.json$/, loaders: ['json'], },
      { test: /\.js$/,  loader: 'eslint', exclude: /node_modules/ },
      { test: /\.js$/,  loader: 'babel', exclude: /node_modules/ },
    ]
  },
  // build breaks on eslint without this workaround
  // https://github.com/MoOx/eslint-loader/issues/23
  eslint: {
    emitWarning: true
  },
  node: {
    __dirname: true,
  },
}

Most helpful comment

Just as a posterity comment in case any one else comes across this in the future. I ran across a very similar issue in one of the projects I was working on due to the implicit addition of 'use strict'; to the top of files from using babel/babel-loader.

If you are lazily writing to the global space (and yes, you shouldn't), you might hit undefined cases for code that otherwise works with webpack.

All 9 comments

Looking at your configuration, it doesn't look like it's a babel-loader problem, however since you're familiar with the code, could you explain why you think is a babel problem? =\

It began happening after I upgraded to Babel 6, and I haven't been able to
find many related issues out there. The only thing that changed about my
build was the Babel config.

That being said, maybe I should open an issue with webpack.

On Sunday, January 17, 2016, Luís Couto [email protected] wrote:

Looking at your configuration, it doesn't look like it's a babel-loader
problem, however since you're familiar with the code, could you explain why
you think is a babel problem? =\

—
Reply to this email directly or view it on GitHub
https://github.com/babel/babel-loader/issues/202#issuecomment-172378624.

@hoodsy did you fix this?

Yes, by adding an alias to redux-devtools (v3).

On Tuesday, February 2, 2016, Mohamed El Mahallawy [email protected]
wrote:

@hoodsy https://github.com/hoodsy did you fix this?

—
Reply to this email directly or view it on GitHub
https://github.com/babel/babel-loader/issues/202#issuecomment-178968411.

What is the full filename of SetCache.js compared to your project? Are you 100% confident you are not transpiling third party code (such as Lodash) with Babel?

@gaearon I exclude node_modules in my loaders, but I can't say I'm 100% confident, as I can't reproduce the issue.

Let’s close then. If you find a way to repro please let us know.

Just as a posterity comment in case any one else comes across this in the future. I ran across a very similar issue in one of the projects I was working on due to the implicit addition of 'use strict'; to the top of files from using babel/babel-loader.

If you are lazily writing to the global space (and yes, you shouldn't), you might hit undefined cases for code that otherwise works with webpack.

the future is not too far off @seancolyer - I've been banging my head on this, I was just happy to randomly see a new comment after frantically reading through this issue for the fifteenth time in the last two days. Seems to be related to https://github.com/webpack/webpack/issues/1963 (for me anyways), which I fixed by using lodash's solution here: https://github.com/lodash/lodash/issues/1916#issuecomment-177659991

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iwotastic picture iwotastic  Â·  4Comments

yuri-karadzhov picture yuri-karadzhov  Â·  3Comments

nemmtor picture nemmtor  Â·  5Comments

CellChen picture CellChen  Â·  3Comments

ghost picture ghost  Â·  4Comments