Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Uncaught ReferenceError: global is not defined #6

Closed
TrySound opened this issue Nov 2, 2015 · 16 comments
Closed

Uncaught ReferenceError: global is not defined #6

TrySound opened this issue Nov 2, 2015 · 16 comments

Comments

@TrySound
Copy link
Member

TrySound commented Nov 2, 2015

browserify defines global as window alias

For example this lib fails in browser
https://github.com/jbraithwaite/scroll-to-y

@Rich-Harris
Copy link
Contributor

Don't have time to implement this right now, but I reckon the best fix is to detect whether global is referenced in any CommonJS modules (this is fairly straightforward thanks to some stuff that got added for 1.2.0), and if so, add an intro to the bundle and add global to each module like so:

// intro for the whole bundle
var commonjsGlobal = typeof window !== 'undefined' ? window :
                     typeof global !== 'undefined' ? global :
                     this;

// individual module
var foo = (function (module, global) {
var exports = module.exports;
/* commonjs module happens */
return module.exports;
}({exports:{}}, commonjsGlobal);

export default foo;

@Rich-Harris
Copy link
Contributor

Added and released as 1.3.0

@TrySound
Copy link
Member Author

TrySound commented Nov 7, 2015

@Rich-Harris Thanks!

@hugomallet
Copy link

Hi,

An issue with Underscore.js that probably relates to this one:

var previousUnderscore = root._;
                               ^

TypeError: Cannot read property '_' of undefined

Reproduce:

  • dependencies
    "rollup": "^0.22.0",
    "rollup-plugin-commonjs": "^1.4.0",
    "rollup-plugin-npm": "^1.1.0",
    "underscore": ">=1.7.0"
  • lib.js
import _ from 'underscore';

export default function foo() {
    return _.map([1, 2, 3], function(num){ return num * 3; });
}
  • rollup-config.js
import commonjs from 'rollup-plugin-commonjs';
import npm from 'rollup-plugin-npm';

export default {
    entry: 'lib.js',
    dest: 'lib-bundle.js',
    format: 'cjs',
    plugins: [
        npm({
            main: true
        }),
        commonjs()
    ]
}
  • execute
rollup -c rollup-config.js
node lib-bundle.js

@Rich-Harris
Copy link
Contributor

@hugomallet This might have been related to #31, and if so would be fixed now – please open a new issue if not

@jstcki
Copy link

jstcki commented Jun 23, 2016

This happens again with v3.0.1 in combination with babel-polyfill:

    if (global._babelPolyfill) { // <--- here
      throw new Error("only one instance of babel-polyfill is allowed");
    }

Works fine with v2.2.1.

@StreetStrider
Copy link

@herrstucki, hello.
I've faced global is not defined issue too. I'm not sure what is the current status of this? I've updated to rollup-plugin-commonjs@3.1.0 and still have this. Should this module convert global to window or there're some reasons not to do it which I don't know about? Can you help with this?

I see some attempts to capture proper global object in my bundle, but seems they are not enough.

@TrySound
Copy link
Member Author

TrySound commented Jul 9, 2016

@StreetStrider this plugin generates this stuff

typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}

Produce please minimal example

@StreetStrider
Copy link

@TrySound, yes I've detected this in my bundle. This expr assigned to name commonjsGlobal. But for some reason some of my bundled deps do not reassign its global refs to commonjsGlobal which leads to an error. I've tried to create minimal reproducible repo now, but everything works OK in it. Looks that something is wrong in my real (and more complex) bundle. Will try to make it reproducible or to find issue on my side.

@TrySound TrySound reopened this Jul 9, 2016
@StreetStrider
Copy link

Got this code transformed into:

        if ( (global.Buffer && global.Buffer.isBuffer(obj)) ||
             (global.ArrayBuffer && obj instanceof ArrayBuffer) ||
             (global.Blob && obj instanceof Blob) ||
             (commonjsGlobal.File && obj instanceof File)
            ) {

in my bundle. Still digging.

@StreetStrider
Copy link

@TrySound, problem dissolves after clear install. Looks like something in caches or elsewhere obscures me. Everything works ok on rollup-plugin-commonjs@3.1.0. whew

@TrySound TrySound closed this as completed Jul 9, 2016
@itsazzad
Copy link

itsazzad commented Feb 7, 2017

Uncaught ReferenceError: global is not defined
    at index.js:388
    at empty (index.js:4)
    at index.js:5
import 'fabric';

// create a wrapper around native canvas element (with id="c")
var canvas = new fabric.Canvas('c');

// create a rectangle object
var rect = new fabric.Rect({
  left: 100,
  top: 100,
  fill: 'red',
  width: 20,
  height: 20
});

// "add" rectangle onto canvas
canvas.add(rect);

@kpgarrod
Copy link

kpgarrod commented Mar 8, 2018

I am getting this same message with the following versions:

    "rollup": "^0.56.5",
    "rollup-plugin-commonjs": "^9.0.0",
    "rollup-plugin-json": "^2.3.0",
    "rollup-plugin-node-builtins": "^2.1.2",
    "rollup-plugin-node-resolve": "^3.2.0"

and this config:

import builtins from 'rollup-plugin-node-builtins';
import commonjs from 'rollup-plugin-commonjs';
import json from 'rollup-plugin-json';
import resolve from 'rollup-plugin-node-resolve';
export default {
  input: 'node-binance-api.js',
  output: {
    name: 'binance',
    file: 'node-binance-api-module.js',
    format: 'iife'
  },
  plugins: [
    commonjs({sourceMap: false}),
    builtins(),
    json({preferConst: true}),
    resolve()
  ]
}

I have tried bundling two public libraries:

  1. https://github.com/jaggedsoft/node-binance-api
  2. https://github.com/ccxt/ccxt

I get the same error with both. Any suggestions please?

@antony
Copy link

antony commented Jul 19, 2019

I get this trying to include

  import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder'

No time to produce a minimal example right now, but I will as soon as possible - just wanted to confirm this as an ongoing issue.

@vitalis-wiens
Copy link

I had the same issue. I have found a workaround(hack).

In rollup.config.js :

output: {
  sourcemap: true,
  format: 'iife',
  name: 'app',
  file: 'public/bundle.js',
  
   // <<<<<< adds this line into the bundle.js >>>>>>>  const global = window;
   intro: 'const global = window;'
		
},

This defines the variable 'global' at the beginning of the bundle.

I am somewhat new to the whole bundling process and if this is conceptually wrong, please let me know.

However, my app works now 👍

@myleftshoe
Copy link

Thanks @vitalis-wiens this fixed it for tonejs:

intro: 'var global = typeof self !== undefined ? self : this;'

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants