nodejs-local-auth icon indicating copy to clipboard operation
nodejs-local-auth copied to clipboard

Error module not found , key.json

Open isaacdarcilla opened this issue 1 year ago • 6 comments

Need help. I am trying to use App router here using NextJS.

Error says Cannot find module

error Error: Cannot find module 'C:\path\to\project\packages\frontend\public\google\client.json' at webpackEmptyContext (C:\path\to\project\packages\frontend\.next\server\app\api\captions\route.js:22:10)

Now, the client.json file is placed in public/google directory and I'm trying to access it in api route.ts

This is the code in route.ts

// app/api/captions/route.ts

import { google } from "googleapis";
import { authenticate } from "@google-cloud/local-auth";
import path from "path";

export async function GET() {

  const auth = await authenticate({
    keyfilePath: path.resolve("public", "google", "client.json"),
    scopes: ["https://www.googleapis.com/auth/youtube"],
  });

  google.options({ auth });
  
  console.log(auth);
}

isaacdarcilla avatar Jan 24 '24 03:01 isaacdarcilla

I am facing the same issue. Works fine in js but not ts

nayyer28 avatar Jan 30 '24 16:01 nayyer28

I am facing the same issue. Works fine in js but not ts

Was able to make this work by using google api client directly.

So instead of that, I did implement it like this,

Using App Router:

import { google } from "googleapis";
import path from "path";

export async function GET(request: NextRequest) {

    const auth = new google.auth.GoogleAuth({
      keyFile: path.resolve("public", "google", "client.json"),
      scopes: [
        "https://www.googleapis.com/auth/youtube",
      ],
    });

    google.options({ auth: auth });
    
    console.log(auth);
 }

isaacdarcilla avatar Jan 30 '24 22:01 isaacdarcilla

Can confirm. This breaks in a fresh TS project.

deltaepsilon avatar Mar 12 '24 12:03 deltaepsilon

I got around it by using the Page Router. App Router will not work with this package.

deltaepsilon avatar Mar 12 '24 13:03 deltaepsilon

I have the same problem on NextJs with the app router. I followed this tutorial to make it work is the same approach as @isaacdarcilla

FerCamposDev avatar Sep 29 '24 02:09 FerCamposDev

I am facing the same issue. Works fine in js but not ts

Was able to make this work by using google api client directly.

So instead of that, I did implement it like this,

import { google } from "googleapis";
import path from "path";

export async function GET(request: NextRequest) {

    const auth = new google.auth.GoogleAuth({
      keyFile: path.resolve("public", "google", "client.json"),
      scopes: [
        "https://www.googleapis.com/auth/youtube",
      ],
    });

    google.options({ auth: auth });
    
    console.log(auth);
 }

Works like a charm. Thx

OndrejHj04 avatar Oct 21 '24 09:10 OndrejHj04