zepto icon indicating copy to clipboard operation
zepto copied to clipboard

Zepto doesn't work with npm and webpack

Open cyrilchapon opened this issue 7 years ago • 19 comments

This issue was closed https://github.com/madrobby/zepto/issues/1222 with no reason :)

import $ from 'zepto'

Leads to

Uncaught TypeError: Cannot read property 'createElement' of undefined


The workaround provided, "use master branch" does not work either, since requiring module from github with npm (npm install madrobby/zepto#master) results in a non-built package, that can't be required, then

import $ from 'zepto'

Leads to

Module not found: Error: Can't resolve 'zepto'


This is dead simple, there is no need for any explaination, but a real need for a git merge and an npm publish basically

Requiring a module from the most popular package manager, then using it through the most popular module bundler does seem like a basic requirement for a 13k stars js library in 2017, doesn't it ?

cyrilchapon avatar Dec 21 '17 07:12 cyrilchapon

TypeScript support would also be nice!

vacekj avatar Mar 24 '18 09:03 vacekj

any progress with this?

shabashj avatar Mar 25 '18 13:03 shabashj

If you want to use Zepto because it's lighter and less bloated than jQuery, as did I, go with a custom jQuery build. Here are the instructions. After building it, require (or import) the resulting file inside of your main script, or directly in webpack. With VSCode, you will still have IntelliSense for jQuery, and your build will only contain what you need.

vacekj avatar Mar 25 '18 14:03 vacekj

OK , thank you.

shabashj avatar Mar 25 '18 15:03 shabashj

This is insane

cyrilchapon avatar Apr 01 '18 10:04 cyrilchapon

Wtf how is this a thing? I just did exactly as you described. Cant believe it doesn't work that easy. I guess It can be added outside the bundle and then "$" set as a global var.

gabriel3000 avatar May 10 '18 14:05 gabriel3000

Zepto is dead to me because of this. Luckily, you don't really need jQuery anymore, it's easier and less painful to use vanilla js

vacekj avatar May 12 '18 12:05 vacekj

same to me

nfwyst avatar May 13 '18 03:05 nfwyst

this workaround gets zepto 1.2.0 (latest npm release) working with webpack:

environment.config.module = {
  rules: [
    {
      test: require.resolve("zepto"),
      use: "imports-loader?this=>window"
    }
  ]
}

then to use zepto as a jquery replacement:

environment.config.resolve = {
  alias: {
    jquery: "zepto"
  }
}

need to install the imports-loader webpack plugin first

linrock avatar Jul 03 '18 03:07 linrock

Same for me, simple import breaks everything. Can't use this library.

npm install zepto
node
> require('zepto')
TypeError: Cannot read property 'createElement' of undefined

AlexMost avatar Jul 09 '18 13:07 AlexMost

Hi @madrobby, can you look at this? It's 2 years old version in npm...

volas avatar Aug 23 '18 00:08 volas

due to the issue, I had to refuse usage of zepto in a large project

SqrTT avatar Aug 23 '18 07:08 SqrTT

To augment sqrTt words, same here, from a CTO

cyrilchapon avatar Aug 23 '18 14:08 cyrilchapon

It still has to work in browsers that don't support import or require, doesn't it? I hate to be that guy, but how is it you have time to b1tch about it on GitHub, but you don't have time for a fork-and-fix?

jbmonroe avatar Oct 10 '18 15:10 jbmonroe

but you don't have time for a fork-and-fix?

And then support/maintain for EOL, while the issue is fixed in master just not released to npm.

See https://github.com/madrobby/zepto/pull/1244

SqrTT avatar Oct 10 '18 16:10 SqrTT

Just be realistic about it guys... there is still no valid replacement for jquery 🤣

leefsmp avatar Oct 29 '18 14:10 leefsmp

If you stick to the evergreen browsers, there's almost no need for jQuery. I haven't needed much more than

const $ = s => Array.from(document.querySelectorAll(s));

for years. About 90% of the main-stream jQuery use can be supplied by functions taking the results of $() and using vanilla JS to get the job done.

jbmonroe avatar Oct 29 '18 14:10 jbmonroe

Works for Me with fuse-box, which is built on typescript and so allows for synthetic imports.

import 'zepto/src/zepto'
import 'zepto/src/event'
import 'zepto/src/fx'

document.addEventListener("DOMContentLoaded", function(event) {
    Zepto("#hi")
        .animate({
        opacity: 0.25, left: '50px',
        color: '#abcdef',
        rotateZ: '45deg', translate3d: '-100px,-100px,-100px'
    }, 10000, 'ease-out')
})

EDIT: I was originally importing the whole thing plus fx. The correct way to do it is to import zepto.js, event.js, and fx.js for the given example.

Something missing from the docs is that these modules have some level of inter-dependency. EG, fx.js depends on events.js

paxperscientiam avatar Jun 10 '21 22:06 paxperscientiam

what a waste of time to attempt using zepto.... I finally get it working the way @paxperscientiam described, and.... I can't even do a query by ID because apparently the id has some forbidden character (the # i suppose? no idea) something that isn't an issue with document.getElementById, unbelievable.....

Sleepful avatar Nov 29 '22 10:11 Sleepful