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

Dynamic require() usage in "modern" build. #128

Open
erictaylor opened this issue Dec 14, 2022 · 3 comments
Open

Dynamic require() usage in "modern" build. #128

erictaylor opened this issue Dec 14, 2022 · 3 comments

Comments

@erictaylor
Copy link

The "modern" build of this library causes problems with some build systems (such as Vite) because of the usage of dynamic requires (ex: https://github.com/saurabhnemade/react-twitter-embed/blob/master/src/components/TwitterTweetEmbed.tsx#L41) since "require" is a Node thing.

When shipping to production you end up getting errors about "require" not being defined.

For my project I've manually patched this problem by creating a Yarn patch and refactored all usage of the require('scriptjs') in effects to use dynamic imports instead:

useEffect(() => {
  // ...
  const loadScript = async () => {
    const scriptModule = await import('scriptjs');
    const script = scriptModule.default;
    
    // ... specific script() call for given useEffect
  };
  
  loadScript();
  
  // ... the useEffect return statement
}, [/* the deps */];

I'm happy to provide a PR to make this change @saurabhnemade if desired.

@asoulliereHT
Copy link

related issue

#105

@qinhua
Copy link

qinhua commented May 27, 2023

For me, simply adding a script tag to import require.min.js in index.html works fine.

@jjsmclaughlin
Copy link

I moved over to react-twitter-widgets https://www.npmjs.com/package/react-twitter-widgets

It's a drop-in replacement which doesn't have this problem.

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

4 participants