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

Loading with webpack #225

Closed
prewk opened this issue Aug 25, 2015 · 33 comments
Closed

Loading with webpack #225

prewk opened this issue Aug 25, 2015 · 33 comments
Milestone

Comments

@prewk
Copy link

prewk commented Aug 25, 2015

Has anyone successfully loaded flickity via npm with webpack?

var Flickity = require('flickity');

Ends up in this, for me:

ERROR in /Users/oskar/src/x/~/flickity/js/drag.js
Module not found: Error: Cannot resolve module 'classie/classie' in /Users/oskar/src/x/node_modules/flickity/js
 @ /Users/oskar/src/x/~/flickity/js/drag.js 7:4-15:6

ERROR in /Users/oskar/src/x/~/flickity/js/flickity.js
Module not found: Error: Cannot resolve module 'classie/classie' in /Users/oskar/src/x/node_modules/flickity/js
 @ /Users/oskar/src/x/~/flickity/js/flickity.js 18:4-28:6

ERROR in /Users/oskar/src/x/~/flickity/js/flickity.js
Module not found: Error: Cannot resolve module 'eventEmitter/EventEmitter' in /Users/oskar/src/x/node_modules/flickity/js
 @ /Users/oskar/src/x/~/flickity/js/flickity.js 18:4-28:6

ERROR in /Users/oskar/src/x/~/flickity/js/player.js
Module not found: Error: Cannot resolve module 'eventEmitter/EventEmitter' in /Users/oskar/src/x/node_modules/flickity/js
 @ /Users/oskar/src/x/~/flickity/js/player.js 7:4-13:6

ERROR in /Users/oskar/src/x/~/flickity/js/lazyload.js
Module not found: Error: Cannot resolve module 'classie/classie' in /Users/oskar/src/x/node_modules/flickity/js
 @ /Users/oskar/src/x/~/flickity/js/lazyload.js 7:4-14:6

ERROR in /Users/oskar/src/x/~/flickity/js/animate.js
Module not found: Error: Cannot resolve module 'get-style-property/get-style-property' in /Users/oskar/src/x/node_modules/flickity/js
 @ /Users/oskar/src/x/~/flickity/js/animate.js 7:4-12:6

ERROR in /Users/oskar/src/x/~/flickity/~/fizzy-ui-utils/utils.js
Module not found: Error: Cannot resolve module 'matches-selector/matches-selector' in /Users/oskar/src/x/node_modules/flickity/node_modules/fizzy-ui-utils
 @ /Users/oskar/src/x/~/flickity/~/fizzy-ui-utils/utils.js 15:4-20:6

ERROR in /Users/oskar/src/x/~/flickity/~/get-size/get-size.js
Module not found: Error: Cannot resolve module 'get-style-property/get-style-property' in /Users/oskar/src/x/node_modules/flickity/node_modules/get-size
 @ /Users/oskar/src/x/~/flickity/~/get-size/get-size.js 241:2-70

ERROR in /Users/oskar/src/x/~/flickity/~/tap-listener/~/unipointer/unipointer.js
Module not found: Error: Cannot resolve module 'eventEmitter/EventEmitter' in /Users/oskar/src/x/node_modules/flickity/node_modules/tap-listener/node_modules/unipointer
 @ /Users/oskar/src/x/~/flickity/~/tap-listener/~/unipointer/unipointer.js 16:4-21:6

ERROR in /Users/oskar/src/x/~/flickity/~/unidragger/~/unipointer/unipointer.js
Module not found: Error: Cannot resolve module 'eventEmitter/EventEmitter' in /Users/oskar/src/x/node_modules/flickity/node_modules/unidragger/node_modules/unipointer
 @ /Users/oskar/src/x/~/flickity/~/unidragger/~/unipointer/unipointer.js 16:4-21:6

In other words, webpack doesn't play nice with the UMD loader for some reason.

With some exports-loader/imports-loader tweaking I was able to load the actual dist/flickity.pkgd.js but then I got stuck on it trying to load jquery-bridget and jquery which I didn't think was an actual dependency.

Is flickity dependent on jquery?

Thanks!

@prewk
Copy link
Author

prewk commented Aug 26, 2015

Got it loading by installing imports-loader and adding this rule to my webpack config: { test: /node_modules\/flickity/, loader: 'imports?define=>undefined' }

In other words: Disabling AMD for Flickity and all of its deps solved my problem.

@prewk prewk closed this as completed Aug 26, 2015
@johnhornsby
Copy link

This worked for me also thank you. { test: /node_modules\/unidragger/, loader: 'imports?define=>undefined' }

@PaulTondeur
Copy link

A better solution might be to load the packaged version instead flickity/dist/flickity.pkgd.js

@prewk
Copy link
Author

prewk commented Oct 1, 2015

@PaulTondeur I went down that route and it's no good. It's full of UMD loaders that for some reason are incompatible with webpack.

@PaulTondeur
Copy link

Really? It seems to be working pretty well for me here. No issues at all.

@prewk
Copy link
Author

prewk commented Oct 1, 2015

Oh, circumstances must've changed since I posted this issue, then. Hopefully the next googler can find a solution in this thread!

@PaulTondeur
Copy link

Nice! That's why I left the comment :-)

@desandro desandro reopened this Oct 2, 2015
@benbonnet
Copy link

hummm… still getting the errors mentionned initially, even with requiring flickity.pkgd.js
Any others tips ?

@prewk
Copy link
Author

prewk commented Nov 16, 2015

Did you try my trick?

{ test: /node_modules\/flickity/, loader: 'imports?define=>undefined' } in the webpack config, and npm install imports-loader if you don't already have it.

@benbonnet
Copy link

sorry, i was confused by the flickity/dist/flickity.pkgd.js mentionned in the middle of the replies, which with your trick results in the errors you mentionned initially.

npm install imports-loader
then
{ test: /node_modules\/flickity/, loader: 'imports?define=>undefined' }
then
var Flickity = require('flickity');
does work

@prewk
Copy link
Author

prewk commented Nov 16, 2015

Great!

desandro added a commit to metafizzy/flickity-docs that referenced this issue Nov 17, 2015
@desandro
Copy link
Member

Added Webpack instructions to docs


Install Flickity and imports-loader with npm.

npm install flickity
npm install imports-loader

In your config file, webpack.config.js, use the imports loader to disable define and set window for flickity modules.

module.exports = {
  module: {
    loaders: [
      {
        test: /flickity/,
        loader: 'imports?define=>false&this=>window'
      }
    ]
  }
};

(This hack is required because of an issue with how Webpack loads dependencies. +1 webpack/webpack#883 to help get this issue addressed.)

You can then require('flickity').

// main.js
var Flickity = require('flickity');

var flkty = new Flickity( '.gallery', {
  // options...
});

Run webpack.

webpack main.js bundle.js

imagesLoaded, asNavFor and jQuery plugin functionality need to be installed separately, similar to using Browserify.

@nickhudkins
Copy link

FYI, this will break if you are using npm3 due to the flattening of dependencies, so your webpack loader will need to look sort of like so:

      {
        test: /(flickity|fizzy-ui-utils|get-size|unipointer)/,
        loader: 'imports?define=>false&this=>window'
      }

@homerjam
Copy link

homerjam commented Jan 7, 2016

FWIW I fixed my issue with this by installing imagesloaded via bower and adding a resolve fallback to bower_components in my webpack config.

resolve: {
    root: 'node_modules',
    fallback: 'bower_components',
}

@johjacb
Copy link

johjacb commented Feb 17, 2016

I followed all of the other instructions on the page, and this is the only one that worked for me.

FYI, this will break if you are using npm3 due to the flattening of dependencies, so your webpack loader will need to look sort of like so:

  {
    test: /(flickity|fizzy-ui-utils|get-size|unipointer)/,
    loader: 'imports?define=>false&this=>window'
  }

desandro added a commit to metafizzy/flickity-docs that referenced this issue Mar 24, 2016
@davepullen19
Copy link

Is it me, or does this fix only work one directory level deep? As soon as I use Flickty in a component e.g. src > components > partials > file.js, I get the error again. Moving the file up to the components directory works. Any ideas?

@desandro
Copy link
Member

desandro commented Apr 19, 2016

I have updated the Flickity Webpack instructions to use resolve.alias rather than imports loader.

// In your config file, webpack.config.js, add these resolve aliases.
module.exports = {
  resolve: {
    alias: {
      'eventEmitter/EventEmitter': 'wolfy87-eventemitter/EventEmitter',
      'get-style-property/get-style-property': 'desandro-get-style-property/get-style-property',
      'matches-selector/matches-selector': 'desandro-matches-selector/matches-selector',
      'classie/classie': 'desandro-classie/classie'
    }
  }
};

These aliases will not be required in the next major version of Flickity, v2.

@davepullen19
Copy link

davepullen19 commented Apr 19, 2016

Thanks.
I tried this as you mentioned it above, but it won't get it working for the deep folder structure.
Again, my folders only go that deep. Importing Flickty on the entry file works fine.

Here is my config file:

import webpack from 'webpack'
import path from 'path'
import lost from 'lost'
import autoprefixer from 'autoprefixer'
import normalizer from 'postcss-normalize'

export default {
    context: path.join(__dirname, 'src/js'),
    entry: [
        'webpack-hot-middleware/client?reload=true',
      './index.js'
    ],
    output: {
      path: path.join(__dirname, '/public/'),
      filename: 'bundle.js',
      publicPath: '/public/'
    },
    plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
    module: {
        loaders: [{
          test: /\.js?$/,
          exclude: /node_modules/,
          loader: 'babel'
        }, { 
            test: /\.json$/, 
            loader: 'json-loader' 
        }, { 
      test: /\.scss$/, 
      loader: 'style-loader!css-loader!postcss-loader!sass-loader'
    }]
    },
  postcss: [
    lost, 
    autoprefixer,
    normalizer
  ],
  resolve: {
    alias: {
      'eventEmitter/EventEmitter': 'wolfy87-eventemitter/EventEmitter',
      'get-style-property/get-style-property': 'desandro-get-style-property/get-style-property',
      'matches-selector/matches-selector': 'desandro-matches-selector/matches-selector',
      'classie/classie': 'desandro-classie/classie'
    }
  }
}

@adamduncan
Copy link

Moving to webpack's resolve.alias as @desandro mentioned in #225 (comment) has resolved my 'Module not found' errors when using npm3.

I was still getting undefined exports.EventEmitter errors in the browser, even once webpack had compiled successfully. If you also include the module.loaders.test suggested by @nickhudkins #225 (comment), then the scope of this seems to set correctly to window in all cases.

@davepullen19
Copy link

how strange. I've tried both of the fixes together, and still get the undefined window variable when importing it into any file that is not the entry file. would you be able to share your config file to see if i have anything glaringly wrong? i imagine you will be using flickity a few folder levels dow
thanks

@adamduncan
Copy link

@gitpullen, including these module and resolve properties got mine back on track:

module.exports = {
  module: {
    loaders: [{
      test: /(flickity|fizzy-ui-utils|get-size|unipointer)/,
      loader: 'imports?define=>false&this=>window'
    }]
  },
  resolve: {
    alias: {
      'eventEmitter/EventEmitter': 'wolfy87-eventemitter/EventEmitter',
      'get-style-property/get-style-property': 'desandro-get-style-property/get-style-property',
      'matches-selector/matches-selector': 'desandro-matches-selector/matches-selector',
      'classie/classie': 'desandro-classie/classie'
    }
  }
};

@davepullen19
Copy link

Cheers.
So I realised it's because it's rendering the components on the server, so of course window doesn't exit.
There's a related issue here - #353

@desandro
Copy link
Member

Flickity v2 is out now and has much improved Webpack integration. No more config required. You can npm install flickity and var Flickity = require('flickity') without any issue. See Flickity - Webpack for details.

@desandro desandro added this to the v2 milestone Jul 11, 2016
@desandro
Copy link
Member

I'm now closing this issue as resolved. If you run into an issue with Webpack and Flickity, please open a new issue.

@lmartins
Copy link

@desandro the workarounds above still relevant for flickity-as-nav-for though.

@zslabs
Copy link

zslabs commented Mar 21, 2018

@lmartins @desandro Have an example of what I'd need to make https://github.com/metafizzy/flickity-fullscreen work with es6 imports? The webpack error looks like it's looking side of flickity-fullscreen for a folder called flickity instead of looking a directory-level up.

ERROR in ./node_modules/flickity-fullscreen/fullscreen.js
Module not found: Error: Can't resolve './flickity' in '/Users/zach/Sites/le/node_modules/flickity-fullscreen'
/Users/zach/Sites/le/node_modules/flickity-fullscreen/flickity doesn't exist
[/Users/zach/Sites/le/node_modules/flickity-fullscreen/flickity]
[/Users/zach/Sites/le/node_modules/flickity-fullscreen/flickity.js]
[/Users/zach/Sites/le/node_modules/flickity-fullscreen/flickity.json]
[/Users/zach/Sites/le/node_modules/flickity-fullscreen/flickity]

Tried with and without the imports-loader (which doesn't seem like it should be needed anymore).

Was attempting to use it like so:

import Flickity from 'flickity-fullscreen';

export default function gallery() {
  const $headerCarousel = document.querySelector('.header-carousel');

  // eslint-disable-next-line no-unused-vars
  const flickity = new Flickity($headerCarousel, {
    freeScroll: true,
    fullscreen: true,
    lazyLoad: true,
    contain: true,
    prevNextButtons: false,
    pageDots: false,
  });
}

@desandro
Copy link
Member

@zslabs Thanks for catching this bug. Looks like the module definitions should be:

// AMD 
'flickity/js/index'
// CommonJS
'flickity'

I'll add this fix in the next release for flickity-fullscreen and flickity-hash in the next week.

@zslabs
Copy link

zslabs commented Mar 21, 2018

@desandro Ah that's great! Thanks so much - was definitely throwing me for a loop 😏

@camaech
Copy link

camaech commented Mar 26, 2018

I'm getting this error:

/node_modules/flickity/js/index.js:39
})( window, function factory( Flickity ) {
ReferenceError: window is not defined
at Object. (/vagrant/sites/roastar.com/node_modules/flickity/js/index.js:39:5)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/webpack.mix.js:4:16)
at Module._compile (module.js:570:32)

When doing 'npm run dev'. Using Laravel webpack.mix.js so that may be part of the issue. Anyone else run into this error?

@desandro
Copy link
Member

@camaech Flickity requires a browser environment. If you are trying to run Flickity server side, you'll need to skip over loading it as it requires window and document and all that good stuff.

@camaech
Copy link

camaech commented Mar 26, 2018

When I use the instructions here (https://flickity.metafizzy.co/api.html#flickity-setjquery), I get the same error. Window is not defined. Not sure what else to try.

@desandro
Copy link
Member

@camaech See #353 for possible solution.

desandro added a commit to metafizzy/flickity-fullscreen that referenced this issue Mar 27, 2018
@desandro
Copy link
Member

Fixes for fullscreen-flickity and flickity-hash has been released.

This issue is now pretty old, covering deprecated instructions which can mislead new users. I'm locking the issue as such. Please open a new issue if you run into trouble with Flickity and Webpack.

@metafizzy metafizzy locked as resolved and limited conversation to collaborators Mar 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests