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

Server Error ReferenceError: Cannot access 'TwitterApiReadWrite' before initialization [bug] #531

Open
dominik-sfl opened this issue May 20, 2024 · 17 comments

Comments

@dominik-sfl
Copy link

Describe the bug
I am getting this error anytime I just import the library & do a simple call: e.g.

let res = await client.v2.tweet("Hello");

Version

  • Node.js version: 20.1
  • Lib version: 1.16.4

Additional context
All of this worked really well before, not sure what's changed? My codebase is pretty much the same.

@wang4621
Copy link

did you find the issue with this?

@adfdev
Copy link

adfdev commented Jun 29, 2024

up

@Adebesin-Cell
Copy link

I'm getting same issue

    const client = new TwitterApi({
      appKey: env.TWITTER_CONSUMER_KEY,
      appSecret: env.TWITTER_CONSUMER_SECRET,
      accessToken: env.TWITTER_ACCESS_TOKEN,
      accessSecret: env.TWITTER_ACCESS_TOKEN_SECRET,
    })

    const rwClient = client.readWrite

    const tweetText = endent`Hello World!`

    try {
      const result = await rwClient.v2.tweet(tweetText)
      console.log('Tweet posted successfully:', result)
    } catch (error) {
      console.error('Error posting tweet:', error)
    }

@clement-faure
Copy link

Same here, using next.js server actions.

const twitterClient = new TwitterApi(twitterAppBearerToken);

@PLhery
Copy link
Owner

PLhery commented Jul 2, 2024

Hello,

This library is not actively maintained since Elon takeover, however, we accept PRs!

Any idea in what version it started though?

@Adebesin-Cell
Copy link

Hi @PLhery,

Damn, that’s sad. PR about improvement?

I’ll inspect and see when it started.

@Shacharai
Copy link

getting the same issue

@leedsrising
Copy link

Did this ever get resolved? have same issue

@jacquesvdhorst
Copy link

Also running into this with my NextJS project

@ninsau
Copy link

ninsau commented Sep 25, 2024

Running into same problem with nextjs 14 server actions. Any solutions?

@ninsau
Copy link

ninsau commented Sep 25, 2024

Running into same problem with nextjs 14 server actions. Any solutions?

I have found the issue. It's a problem with bundling. Specifically turbo. If you're using nextjs and you're builing with --turbo, simply remove that.

"dev": "next dev --turbo" should become "dev": "next dev",

const postTweet2 = async (text: string) => {
  const userClient = new TwitterApi({
    appKey: process.env.TWITTER_CONSUMER_KEY!,
    appSecret: process.env.TWITTER_CONSUMER_SECRET!,
    accessToken: process.env.TWITTER_ACCESS_TOKEN!,
    accessSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET!,
  });

  try {
    const tw = userClient;

    const tweet = await tw.v2.tweet(text);
  } catch (error) {
    console.error("Error posting tweet:", error);
    throw error;
  }
};

Describe the bug I am getting this error anytime I just import the library & do a simple call: e.g.

let res = await client.v2.tweet("Hello");

Version

  • Node.js version: 20.1
  • Lib version: 1.16.4

Additional context All of this worked really well before, not sure what's changed? My codebase is pretty much the same.

@jacquesvdhorst
Copy link

Just to confirm @ninsau's solution worked for me! Thanks for the suggestion

@paaax
Copy link

paaax commented Oct 29, 2024

I can confirm in nextjs 15 using turbopack causes that issue. Removing --turbopack did the trick. Thank you @ninsau

@dsldsl
Copy link

dsldsl commented Nov 7, 2024

It looks like there's an actual fix in PR but needs to TS updates : #544

Who's the author here? All i see is the integration bot

@PLhery
Copy link
Owner

PLhery commented Nov 7, 2024

Actually, I asked an AI SWE agent (devin) to fix the issue see if it leads to somewhere. But it did not do the right thing, closing the PR.

I think for now not using --turbo is the best fix, it seems to be mostly an issue from turbopack.

@nikshepsvn
Copy link

anyone figure out a way to get it to work while not losing turbo?

@lucassimao
Copy link

add "twitter-api-v2" to the serverExternalPackages array in your next.config.ts file

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