junhoyeo / threads-api

Unofficial, Reverse-Engineered Node.js/TypeScript client for Meta's Threads. Web UI Included.
https://threads.junho.io
MIT License
1.58k stars 135 forks source link

Module not found: Can't resolve `fs` (Can't use the API on a NextJS Project) #98

Closed kevinfosse closed 12 months ago

kevinfosse commented 12 months ago

Hi, I'm coming to you because I'd like to integrate the API into a NextJS project, but I'm getting an error when I try to use it and I don't know where it's coming from...

Failed to compile

./node_modules/threads-api/build/threads-api.js:1:175
Module not found: Can't resolve 'fs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/threads-api/build/index.js
./app/api/getUser.tsx
./app/page.tsx

This error occurred during the build process and can only be dismissed by fixing the error.

Here is my code :

import { NextResponse } from 'next/server';
import { ThreadsAPI } from 'threads-api';

export const getUser = async (): Promise<NextResponse> => {
  const threadsAPI = new ThreadsAPI();
  const username: string = 'zuck';
  const userID: string | null = await threadsAPI.getUserIDfromUsername('unyxes');

  if (!userID) {
    console.log(NextResponse.json({})); // Log the empty JSON response
  }

  const user = await threadsAPI.getUserProfile(username, userID as string);

    console.log(NextResponse.json({ user }));
};
kevinfosse commented 12 months ago

I"m on Next.js v13.4.9 Node v20.0.0

junhoyeo commented 12 months ago

omg, will check. i had some personal issues yesterday and couldn't responded well sorry

junhoyeo commented 12 months ago

@kevinfosse i think this is now resolved in the latest release. can you try again after updating to v1.3.1?

khuepm commented 12 months ago

@junhoyeo I got error on newest version: "version": "1.3.1"

Module not found: Can't resolve 'threads-api'
https://nextjs.org/docs/messages/module-not-found
kevinfosse commented 12 months ago

@junhoyeo Using "threads-api": "^1.3.1",

In React 18.2.0

ERROR in ./node_modules/mime-types/index.js 15:14-37 Module not found: Error: Can't resolve 'path' in 'C:\Users\kevinf\Desktop\threads\testv2\node_modules\mime-types'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:

ERROR in ./node_modules/threads-api/build/threads-api.js 522:21-34 Module not found: Error: Can't resolve 'fs' in 'C:\Users\kevinf\Desktop\threads\testv2\node_modules\threads-api\build'

image

kevinfosse commented 12 months ago

I don't know if it can help you but in your thread-api package.json i don't see "fs" image

junhoyeo commented 12 months ago

@khuepm Didn't know that meme-types was using fs lol... we changed that to mrmime in #109. Can you update to v1.3.2 (just been deployed) and try again?

junhoyeo commented 12 months ago

@junhoyeo I got error on newest version: "version": "1.3.1"

Module not found: Can't resolve 'threads-api'
https://nextjs.org/docs/messages/module-not-found

@khuepm Can you check if the package is installed correctly?

kevinfosse commented 12 months ago

@junhoyeo I tried the version 1.3.2, Mime-types no longer causes problems. Now i only get the problem with fs

junhoyeo commented 12 months ago

@kevinfosse I'll research. Is it the same error/callstack as the original issue you first filed? (./node_modules/threads-api/build/threads-api.js:1:175 / Module not found: Can't resolve 'fs')

Meanwhile, here's a quick fix:

// (next.config.js)
// ...
 webpack: (config) => {
      config.resolve.fallback = {
        ...config.resolve.fallback,
        fs: false,
      }
}
kevinfosse commented 12 months ago

This is the error i got :

ERROR in ./node_modules/threads-api/build/threads-api.js 567:21-34 Module not found: Error: Can't resolve 'fs' in 'C:\Users\kevinf\Desktop\threads\testv2\node_modules\threads-api\build'

Thanks for your quick fix, will try ASAP

kevinfosse commented 12 months ago

Hello, just a little update, your workaround work !

Thanks you !