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

Cannot find module 'tween' when requiring scroll-to server side #9

Open
pixelwhip opened this issue Mar 3, 2016 · 9 comments
Open

Comments

@pixelwhip
Copy link

I'm using scroll-to-element in a React app that pre-renders on the server. When requiring scroll-to-element, which is dependent on scroll-to, I get Error: Cannot find module 'tween'.

I believe this is because scroll-to requires component-tween via var Tween = require('tween'); which works in browser environments because of the following in package.json.

  "browser": {
    "raf": "component-raf",
    "tween": "component-tween"
  },

everything works fine in the client, but the application errors out when running on the server because node doesn't know what tween is.

@pixelwhip pixelwhip changed the title Cannot find module 'tween' requiring scroll to server side Cannot find module 'tween' when requiring scroll-to server side Mar 3, 2016
@nitruxa
Copy link

nitruxa commented Apr 28, 2016

can someone take an action on this? thanks

@5punk
Copy link

5punk commented Nov 30, 2016

+1

Any help, @TooTallNate?

@oori
Copy link

oori commented Jun 27, 2017

You can bypass this issue using conditional require. instead of import('react-scroll-to-component'), you require('react-scroll-to-component') in your componentDidMount, so it only happens on the client.

see: vercel/next.js#219 (comment)
It's a hack, but works well..

@fritz-c
Copy link

fritz-c commented Feb 25, 2018

On the off chance that anyone using Gatsby finds their way here, putting this snippet into gatsby-node.js solved it for me:

exports.modifyWebpackConfig = ({ config, stage }) => {
  if (stage === 'build-html') {
    config.loader('null', {
      test: /scroll-to-element/,
      loader: 'null-loader',
    });
  }
};

@johnferrie
Copy link

Thanks, @fritz-c this solved it for me but I had to change /scroll-to-element/ to /scroll-to/

exports.modifyWebpackConfig = ({ config, stage }) => {
  if (stage === 'build-html') {
    config.loader('null', {
      test: /scroll-to/,
      loader: 'null-loader',
    });
  }
};

cheshire137 added a commit to cheshire137/competiwatch-desktop that referenced this issue Jul 23, 2018
@coreyward
Copy link

@tj Any chance you'd be willing to drop this tiny shortcut in favor of saving hundreds of people time in awkward workarounds?

@SimonSomlai
Copy link

For people running into this with jest testing. Add this to your jest.config.js

moduleNameMapper: { "^react-scroll-to-component$": "identity-obj-proxy", },

@nnattawat
Copy link

@SimonSomlai I did what you mention and it works until my jest test actually triggers scrollTo() in the component file. I fix it by using my mock file instead of dentity-obj-proxy.

// In jest.config.js
moduleNameMapper: { "^react-scroll-to-component$": "/__mock__/react-scroll-to-component", },

// in rootDir/__mock__/react-scroll-to-compone.js 
module.exports = jest.fn();

@ealmario
Copy link

ealmario commented Oct 8, 2020

Anybody have an answer for this? Mine isn't working on Gatsby.

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

10 participants