-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Hammer.js fails in node since window is undefined #930
Comments
You can fix this by shimming
Or:
|
Trying to shim this using webpack's |
Unfortunately... it's not quite as simple as passing |
The only way we could really make this work and what we have discussed to fix this in another issue i cant seem to find is to just noop if there is not a document and window. Hammer cannot operate without them. |
@arschmitz Thanks for the reply. The noop approach is more or less what I was thinking about. Alternatively I could perform a conditional import using a I'm new to hammer and not really familiar with the codebase. Any tips on how best to noop this? I could add conditionals here and there but that doesn't seem very elegant... |
@arschmitz was it #849 Remove global dependency to window and document ? |
Here is the jQuery file I was looking at https://github.com/jquery/jquery/blob/master/src/wrapper.js |
@joshunger yes thats exactly how it should be done. I'm also a member of the jQuery team :-) the only difference here is how we break up the wrapper we use 2 files beginning and end on hammer |
Submitted PR #973 that fixes this using the jquery style approach suggested by @joshunger. Works well. |
Well, so this project doesn't support server side rendering? 🚶 |
any progress ? |
For those who use Webpack there's a simple workaround:
mocks/HammerMock module:
you will probably need to adjust the code above considering methods you use. |
@udivankin Thanks for posting the workaround; however, what do you mean by "mocks/HammerMock" module? Is this a module that exists somewhere? Or something one must create? I'm confused. |
@merges that's an ES6 module somewhere in your project (in my case I put it in mocks folder and named HammerMock,js). Webpack's NormalModuleReplacementPlugin will just replace all occurrences of |
you can also use require.extensions to patch out hammerjs. |
@graingert could u possibly explain further on how to do that |
I am also running into this problem. Or would it be possible to update Hammer-js such that it does not access window handle within export code? (because that code gets invoked during SSR initialization, but in reality window handle is needed later on (as normally folks that rely on Hammer.JS (eg react-resize-layout), call Hammer in componentDidMount (which happens on client side ). For completeness, this suggestion did not work for me, because (still a guess), my SSR build/release pipeline is using Razzle (which underneath creates JS chunks (not one big bundle) for efficiency) |
I had a similar issue with velocity-animate and (crudely) solved it via string-replace-loader the concept is just
and the loader config test: /\.js$/,
loader: 'string-replace-loader',
options: {
search: "import hammerjs from 'hammerjs'\n",
replace: '',
} this is working for me in my vue ssr setup currently |
Got a fix. I downloaded the The reason is because Node. When youre in node it does not know what window is. You can test it in youre terminal. Here is the example:
|
thank you @polymer940c , is there a way to override/polyfill hammer in a way that would not require its source code change in the node_modules? thx again for the suggestion! |
In case anyone come here for angular ssr issue, you can use below webpack config for your server ts build. { Similarly: https://medium.com/@puresmash/solve-hammer-js-issue-on-ssr-project-2e79664a7196 @egjs/hammerjs will work also, but it's not working properly with slider on mobile in my project. |
Another solution is to load hammer on demand where you need it, so that you don't have to update hammer.min.js:
|
So this issue is 4 years old and still no solution? 🙄 |
After installing below 2 dependencies, I was able to launch angular universal.
|
Any solution now ?? |
I solved this error in my case. Just check if it is browser then only import hammerjs. Basically
and again check for browser when you are using hammerjs module. It worked |
Hammer.js fails in node since window is undefined. Also document.
Line 2568 assumes window -
https://github.com/hammerjs/hammer.js/blob/master/hammer.js#L2568
We're trying to run a bundle produced by webpack in the web as well as node targets. It would be helpful to include the same bundle even though we're not using hammer.js on the node side.
jQuery does something like -
typeof window !== "undefined" ? window : this
The text was updated successfully, but these errors were encountered: