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

UMD build can't be used in node env #6784

Open
vreshch opened this Issue Mar 17, 2018 · 7 comments

Comments

Projects
None yet
6 participants
@vreshch

vreshch commented Mar 17, 2018

Ehen using build with libraryTarget: 'umd', targeted build in node enviroment; getting:
AR:
Node give "ReferenceError: window is not defined"
ER:
no issues in node & browser

Not possible to set target param to match both platforms;

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.
Webpack version 4.1.1
It's due to webpackUniversalModuleDefinition - "this" should be as param instead of window

@montogeek

This comment has been minimized.

Member

montogeek commented Mar 17, 2018

It looks like you just deleted our lovely crafted issue template. It was there for good reasons. Please help us solving your issue by answering the questions asked in this template.

@vreshch

This comment has been minimized.

vreshch commented Mar 18, 2018

Do you want to request a feature or report a bug?
bug

What is the current behavior?
Then using build with libraryTarget: 'umd', targeted build in node enviroment; getting:
Node give "ReferenceError: window is not defined"

If the current behavior is a bug, please provide the steps to reproduce.

  1. Create UMD library with config:
    output: { path: '...', library: 'library', libraryTarget: 'umd', umdNamedDefine: true },
  2. Require require the library from nodejs
    Result: "ReferenceError: window is not defined"

What is the expected behavior?
no issues in node & browser
UMD library work in different platforms;

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.

Webpack version 4.1.1
It's due to webpackUniversalModuleDefinition - "this" should be as param instead of window

@MarkRabey

This comment has been minimized.

MarkRabey commented Mar 20, 2018

I'm having the same issue. My output is:

(function webpackUniversalModuleDefinition(root, factory) {
	if(typeof exports === 'object' && typeof module === 'object')
		module.exports = factory();
	else if(typeof define === 'function' && define.amd)
		define([], factory);
	else {
		var a = factory();
		for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
	}
})(window, function() {
return /******/ (function(modules) {
...

That window there seems to be the issue. The documentation suggests that instead of window, I should have typeof self !== 'undefined' ? self : this. If I change that manually my library works in both the browser and Node environments. Obviously changing it myself is not the solution.

@tooleks

This comment has been minimized.

tooleks commented Mar 24, 2018

Same issue here with webpack@4.2.0.

@ScottRudiger

This comment has been minimized.

ScottRudiger commented Mar 25, 2018

I solved this by setting globalObject to 'typeof self !== \'undefined\' ? self : this' in output; e.g.,

output: {
...
  globalObject: 'typeof self !== \'undefined\' ? self : this',
...
},

Edit: But, I agree it should output this way by default whenever libraryTarget is set to umd (especially since that's what appears in the docs). It certainly caused some time of confusion/frustration.

@gpoitch gpoitch referenced this issue Mar 25, 2018

Merged

Fix UMD Builds #17

@MarkRabey

This comment has been minimized.

MarkRabey commented Mar 26, 2018

I solved it by switching to Rollup.

aullman added a commit to aullman/opentok-layout-js that referenced this issue Jul 9, 2018

@egoroof

This comment has been minimized.

egoroof commented Oct 4, 2018

omg updated from webpack 3 to webpack 4 and got this issue
no updates here?!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment