googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.26k stars 1.91k forks source link

Module not found when initializing Chat API #3425

Open mcarrf90-bz opened 5 months ago

mcarrf90-bz commented 5 months ago

Environment details

Steps to reproduce

I'm getting the following error when trying to use the chat API. When I run npm run start I get:

Error: Cannot find module 'C:\Users\Martin Carrasco\Desktop\BurnoutZero\boz-google-chat-app\node_modules\https-proxy-agent\dist\index.js'. Please verify that the package.json has a valid "main" entry   
    at Module._findPath (node:internal/modules/cjs/loader:720:18)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1135:27)
    at Module._load (node:internal/modules/cjs/loader:990:27)
    at Module.require (node:internal/modules/cjs/loader:1237:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (C:\Users\Martin Carrasco\Desktop\BurnoutZero\boz-google-chat-app\node_modules\gaxios\build\src\gaxios.js:27:29)
    at Module._extensions..js (node:internal/modules/cjs/loader:1437:10)
    at Module.load (node:internal/modules/cjs/loader:1212:32) {
  code: 'MODULE_NOT_FOUND',
  path: 'C:\\Users\\Martin Carrasco\\Desktop\\BurnoutZero\\boz-google-chat-app\\node_modules\\https-proxy-agent\\package.json',
  requestPath: 'https-proxy-agent'
}

And here is my code:

const chat = require('@googleapis/chat');

async function createMessage() {
  const auth = new chat.auth.GoogleAuth({

    // Specify service account details.
    keyFilename: 'credentials.json',

    // Specify required scopes.
    scopes: ['https://www.googleapis.com/auth/chat.bot']

  });
  const authClient = await auth.getClient();

  // Create the Chat API client and authenticate with the service account.
  const chatClient = await chat.chat({
    version: 'v1',
    auth: authClient
  });

  // Create a Chat message.
  const result = await chatClient.spaces.messages.create({

    // The space to create the message in.
    //
    // Replace SPACE_NAME with a space name.
    // Obtain the space name from the spaces resource of Chat API,
    // or from a space's URL.
    parent: 'spaces/AAAAxqmzN8I',

    // The message to create.
    requestBody: { 'text': 'Hello, world!' }

  });
  return result;
}

// Execute function then print details about the created message.
(async() => {
    const r = await createMessage();
    console.log(r);
})()

I tried using all this node versions: