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

global is not defined #11

Closed
rioukevin opened this issue Dec 26, 2019 · 6 comments
Closed

global is not defined #11

rioukevin opened this issue Dec 26, 2019 · 6 comments

Comments

@rioukevin
Copy link

rioukevin commented Dec 26, 2019

Hello,

I use vuepress (Vuejs) that use library using has-symbols, an error occured:
global is not defined in the library script

'use strict';

// V Error is throw next line V
var origSymbol = global.Symbol;
var hasSymbolSham = require('./shams');

module.exports = function hasNativeSymbols() {
	if (typeof origSymbol !== 'function') { return false; }
	if (typeof Symbol !== 'function') { return false; }
	if (typeof origSymbol('foo') !== 'symbol') { return false; }
	if (typeof Symbol('bar') !== 'symbol') { return false; }

	return hasSymbolSham();
};

So to correct it, I add in my scripts

window.global = window

I see that this error is throwed with angular also, so it looks that more and more framework doesn't support global 😉

Can you add a check on window.global to avoid create an error when it isn't define ?
Just emit a default value when not defined ?

Thank you 😃 😄

Have a nice day and be happy !
Kévin

@ljharb
Copy link
Member

ljharb commented Dec 26, 2019

global is always defined in node, this is a node module, and bundlers should be transforming globsl to window for browsers.

If your bundler isn’t doing that, please file an issue on it so it can be fixed.

@ljharb ljharb closed this as completed Dec 26, 2019
@ljharb
Copy link
Member

ljharb commented Dec 26, 2019

Duplicate of #6. Duplicate of #4.

@JounQin
Copy link

JounQin commented Feb 26, 2021

Why not just writing browser friendly check? I've been suffered from this.

@ljharb
Copy link
Member

ljharb commented Feb 26, 2021

@JounQin global is a browser-friendly check in a node module, when used with a non-broken node module bundler.

This package is a node module, and is explicitly not intended to be used anywhere but in node, or in a browser via a properly working node module bundler - which requires that it support global, among a number of other things.

@JounQin
Copy link

JounQin commented Feb 26, 2021

Maybe you're right, but by just adding typeof Symbol !== 'undefined' && Symbol, a lot of issues will be resolved thanks to this package.

@ljharb
Copy link
Member

ljharb commented Feb 27, 2021

All that would achieve is to make it slightly easier for bundlers to do the wrong thing. You'll still find yourself suffering due to another package - it'd just move the problem.

That said, it's fair that the typeof version would work identically, so I suppose I'll go ahead and do it. I strongly suggest you also spend effort trying to fix the broken node module bundlers that fail to polyfill global.

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

No branches or pull requests

3 participants