Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught ReferenceError: global is not defined #6

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

Comments

Projects
None yet
7 participants
@TrySound
Copy link
Member

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

This comment has been minimized.

Copy link
Contributor

commented Nov 2, 2015

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

This comment has been minimized.

Copy link
Contributor

commented Nov 7, 2015

Added and released as 1.3.0

@TrySound

This comment has been minimized.

Copy link
Member Author

commented Nov 7, 2015

@Rich-Harris Thanks!

@hugomallet

This comment has been minimized.

Copy link

commented Jan 6, 2016

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

This comment has been minimized.

Copy link
Contributor

commented Jan 19, 2016

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

@herrstucki

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Jul 9, 2016

@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

This comment has been minimized.

Copy link
Member Author

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

This comment has been minimized.

Copy link

commented Jul 9, 2016

@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

This comment has been minimized.

Copy link

commented Jul 9, 2016

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

This comment has been minimized.

Copy link

commented Jul 9, 2016

@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 Jul 9, 2016

@itsazzad

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.