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 · 12 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

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