node-twitter-api-v2
node-twitter-api-v2 copied to clipboard
Server Error ReferenceError: Cannot access 'TwitterApiReadWrite' before initialization [bug]
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.
did you find the issue with this?
up
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)
}
Same here, using next.js server actions.
const twitterClient = new TwitterApi(twitterAppBearerToken);
Hello,
This library is not actively maintained since Elon takeover, however, we accept PRs!
Any idea in what version it started though?
Hi @PLhery,
Damn, that’s sad. PR about improvement?
I’ll inspect and see when it started.
getting the same issue
Did this ever get resolved? have same issue
Also running into this with my NextJS project
Running into same problem with nextjs 14 server actions. Any solutions?
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.
Just to confirm @ninsau's solution worked for me! Thanks for the suggestion
I can confirm in nextjs 15 using turbopack causes that issue. Removing --turbopack did the trick. Thank you @ninsau
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
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.
anyone figure out a way to get it to work while not losing turbo?
add "twitter-api-v2" to the serverExternalPackages array in your next.config.ts file
if you've updated to latest NextJs; it's now serverExternalPackages instead of experiment.serverComponentsExternalPackages
This should be fixed in 1.27.0 (Also, I couldn't repro on Next 15, only on Next 14)