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

TypeError: Cannot read property 'createElement' of undefined #1222

Closed
diamont1001 opened this issue Aug 24, 2016 · 17 comments
Closed

TypeError: Cannot read property 'createElement' of undefined #1222

diamont1001 opened this issue Aug 24, 2016 · 17 comments

Comments

@diamont1001
Copy link

What's happening?

There is an error testing here: https://tonicdev.com/npm/zepto

@temoto
Copy link

temoto commented Sep 22, 2016

What's happening is scope wrapper (function(window){ ... }()) received empty object for some reason.
This also reproduces under browserify builds.

Mad workaround (then you'll need to add more):

  Object.prototype.document = window.document;
  Object.prototype.location = window.location;
  var $ = require('zepto').$;
  delete(Object.prototype.document);
  delete(Object.prototype.location);

@ringcrl
Copy link

ringcrl commented Oct 26, 2016

@temoto
work for me

@positlabs
Copy link
Contributor

I've seen this happen in commonjs / browserify environments before. The issue is that the scope wrapper doesn't live in window... rather it is wrapped in another scope wrapper, so this becomes a reference to the outer wrapper, and not window as is assumed by the zepto scope wrapper.

(function(global, factory) {
  if (typeof define === 'function' && define.amd)
    define(function() { return factory(global) })
  else
    factory(global)

// this should window
// }(this, function(window) {
}(window, function(window) {

@positlabs
Copy link
Contributor

Same issue as #1210

@Evans-Cai
Copy link

Evans-Cai commented Dec 2, 2016 via email

@temoto
Copy link

temoto commented Dec 2, 2016

Why wouldn't one just use name window inside any/all wrappers?

@positlabs
Copy link
Contributor

positlabs commented Dec 2, 2016

I've created a PR here: #1244

@madrobby
Copy link
Owner

madrobby commented Aug 6, 2017

This should work now on the master branch. Please try again and feel free to reopen this issue if it doesn't work correctly.

@madrobby madrobby closed this as completed Aug 6, 2017
@KroniK907
Copy link

I don't think this works properly, at least not when using webpack. I'm still getting the same TypeError.

@Lenic
Copy link

Lenic commented Aug 23, 2017

same to me...

@positlabs
Copy link
Contributor

positlabs commented Aug 23, 2017 via email

@braxnu
Copy link

braxnu commented Aug 26, 2017

Hi, please do npm run build on this https://github.com/braxnu/zepto-factory-scoping-fail-proof

@positlabs
Copy link
Contributor

@braxnu please use master branch, not v1.2.0. Zepto hasn't had a version bump since this PR was merged.

@madrobby do you think it's possible to bump to v1.2.1, then publish to npm?

@ataylorme
Copy link

@madrobby do you think it's possible to bump to v1.2.1, then publish to npm?

+1 to this. I'm pulling in Zepto with Browserify and am seeing this error. A new version published to NPM would be appreciated.

@p
Copy link

p commented Dec 10, 2017

@madrobby Can we get a fixed version on npm please?

For others experiencing this issue, #1244 contains a workaround for webpack.

@chanelnumberfive
Copy link

i agree with positlabs's idea ,use window instead of this and it work when i using webpack!

(function(global, factory) {
if (typeof define === 'function' && define.amd)
define(function() { return factory(global) })
else
factory(global)

// this should window
// }(this, function(window) {
}(window, function(window) {

@LMCom
Copy link

LMCom commented Oct 26, 2020

I'm using webpack with Zepto 1.2.0. The browser console:
Uncaught TypeError: document is undefined
Zepto zepto.js:23

Line 23 is the last line in the following snippet. window cannot be undefined, otherwise there would have been a null reference in line 4. It is an empty object and does not have the property document. I verified this.
I'm importing Zepto via import 'zepto';. Is this correct?

(this, function(window) {
  var Zepto = (function() {
  var undefined, key, $, classList, emptyArray = [], concat = emptyArray.concat, filter = emptyArray.filter, slice = emptyArray.slice,
    document = window.document,
    elementDisplay = {}, classCache = {},
    cssNumber = { 'column-count': 1, 'columns': 1, 'font-weight': 1, 'line-height': 1,'opacity': 1, 'z-index': 1, 'zoom': 1 },
    fragmentRE = /^\s*<(\w+|!)[^>]*>/,
    singleTagRE = /^<(\w+)\s*\/?>(?:<\/\1>|)$/,
    tagExpanderRE = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig,
    rootNodeRE = /^(?:body|html)$/i,
    capitalRE = /([A-Z])/g,

    // special attributes that should be get/set via method calls
    methodAttributes = ['val', 'css', 'html', 'text', 'data', 'width', 'height', 'offset'],

    adjacencyOperators = [ 'after', 'prepend', 'before', 'append' ],
    table = document.createElement('table'),

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