node-twitter-api-v2 icon indicating copy to clipboard operation
node-twitter-api-v2 copied to clipboard

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

Open dominik-sfl opened this issue 1 year ago • 17 comments

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.

dominik-sfl avatar May 20 '24 08:05 dominik-sfl

did you find the issue with this?

wang4621 avatar Jun 14 '24 23:06 wang4621

up

adfdev avatar Jun 29 '24 14:06 adfdev

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)
    }

Adebesin-Cell avatar Jul 01 '24 12:07 Adebesin-Cell

Same here, using next.js server actions.

const twitterClient = new TwitterApi(twitterAppBearerToken);

clement-faure avatar Jul 02 '24 08:07 clement-faure

Hello,

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

Any idea in what version it started though?

plhery avatar Jul 02 '24 14:07 plhery

Hi @PLhery,

Damn, that’s sad. PR about improvement?

I’ll inspect and see when it started.

Adebesin-Cell avatar Jul 02 '24 16:07 Adebesin-Cell

getting the same issue

Shacharai avatar Aug 09 '24 15:08 Shacharai

Did this ever get resolved? have same issue

leedsrising avatar Sep 24 '24 17:09 leedsrising

Also running into this with my NextJS project

jacquesvdhorst avatar Sep 24 '24 17:09 jacquesvdhorst

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

ninsau avatar Sep 25 '24 01:09 ninsau

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.

ninsau avatar Sep 25 '24 01:09 ninsau

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

jacquesvdhorst avatar Oct 04 '24 14:10 jacquesvdhorst

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

paaax avatar Oct 29 '24 05:10 paaax

It looks like there's an actual fix in PR but needs to TS updates : https://github.com/PLhery/node-twitter-api-v2/pull/544

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

dsldsl avatar Nov 07 '24 00:11 dsldsl

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.

plhery avatar Nov 07 '24 13:11 plhery

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

nikshepsvn avatar Nov 18 '24 01:11 nikshepsvn

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

lucassimao avatar Nov 22 '24 17:11 lucassimao

if you've updated to latest NextJs; it's now serverExternalPackages instead of experiment.serverComponentsExternalPackages

danielAsaboro avatar Jul 06 '25 14:07 danielAsaboro

This should be fixed in 1.27.0 (Also, I couldn't repro on Next 15, only on Next 14)

plhery avatar Sep 05 '25 00:09 plhery