admin管理员组

文章数量:1399983

I keep getting this error in the console when trying to import either of these libraries. I'm not sure if there's something I need to install or if using "require" is needed for them.Here's the code in question:

(Not shown, but fs is imported earlier in the code)


// uploads CSV files to Google Drive(does nothing for now, sorry!)
// link to example: .js
// link to stackoverflow question: 
// link to node.js blog: 
async function uploadToDrive(folderId){

 console.log("messing up here?");
  const{GoogleAuth} = await import('google-auth-library');
 const{google} = await import("googleapis")

  // get credentials and build service
  // get proper auth mechanism?(not sure what that means, look up later)
  const auth = new GoogleAuth({scopes: ".file"});
  const service = google.drive({version: "v3", auth});

  //set folderId and upload csv
  folderId = "1HyC6zKH98n0OzDhmhKWJkupdxY-Knd1k";

  // copied code from downladData finction
  if (selectedEntry === undefined) return;
  if (downloadLink === undefined) return; // Make sure the link exists
  downloadLink.href = widgets.makeDownloadLink({ header: selectedEntry.header, values: filterRecords(true) });

  const fileMetadata = {
    Name: selectedEntry.header,
    parents: [folderId]
  };

  const media = {
    mimeType: "text/csv",
    body: fs.createReadStream(`files/${downloadLink.href}`),
  };

  try {
    const file = await service.files.create({
      requestBody: fileMetadata,
      media: media,
      fields: "id",
    });
    console.log("File Id:", file.data.id);
    return file.data.id;
  } catch (err) {
    // TODO(developer) - Handle error
    throw err;
  }

and the error log from the console:

chunk-OCKVXT67.js?v=fbea541c:2930 <Suspense> is an experimental feature and its API will likely change.
InspectorContainer.vue:35 messing up here?
chunk-APNZTO2G.js?v=fbea541c:16065 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'isTTY')
    at _Colours.isEnabled (chunk-APNZTO2G.js?v=fbea541c:16065:23)
    at _Colours.refresh (chunk-APNZTO2G.js?v=fbea541c:16068:37)
    at node_modules/google-logging-utils/build/src/colours.js (chunk-APNZTO2G.js?v=fbea541c:16109:13)
    at __require (chunk-4RDJM6KU.js?v=fbea541c:18:50)
    at node_modules/google-logging-utils/build/src/logging-utils.js (chunk-APNZTO2G.js?v=fbea541c:16157:21)
    at __require (chunk-4RDJM6KU.js?v=fbea541c:18:50)
    at node_modules/google-logging-utils/build/src/index.js (chunk-APNZTO2G.js?v=fbea541c:16405:18)
    at __require (chunk-4RDJM6KU.js?v=fbea541c:18:50)
    at node_modules/gcp-metadata/build/src/index.js (chunk-APNZTO2G.js?v=fbea541c:16446:18)
    at __require (chunk-4RDJM6KU.js?v=fbea541c:18:50)


(Note: I'm using node.js and vue).

I keep getting this error in the console when trying to import either of these libraries. I'm not sure if there's something I need to install or if using "require" is needed for them.Here's the code in question:

(Not shown, but fs is imported earlier in the code)


// uploads CSV files to Google Drive(does nothing for now, sorry!)
// link to example: https://developers.google/drive/api/guides/folder#node.js
// link to stackoverflow question: https://stackoverflow/questions/51584732/create-folder-and-upload-file-to-google-drive-from-typescript-cannot-compile
// link to node.js blog: https://nodejs./en/blog/announcements/v22-release-announce
async function uploadToDrive(folderId){

 console.log("messing up here?");
  const{GoogleAuth} = await import('google-auth-library');
 const{google} = await import("googleapis")

  // get credentials and build service
  // get proper auth mechanism?(not sure what that means, look up later)
  const auth = new GoogleAuth({scopes: "https://www.googleapis/auth/drive.file"});
  const service = google.drive({version: "v3", auth});

  //set folderId and upload csv
  folderId = "1HyC6zKH98n0OzDhmhKWJkupdxY-Knd1k";

  // copied code from downladData finction
  if (selectedEntry === undefined) return;
  if (downloadLink === undefined) return; // Make sure the link exists
  downloadLink.href = widgets.makeDownloadLink({ header: selectedEntry.header, values: filterRecords(true) });

  const fileMetadata = {
    Name: selectedEntry.header,
    parents: [folderId]
  };

  const media = {
    mimeType: "text/csv",
    body: fs.createReadStream(`files/${downloadLink.href}`),
  };

  try {
    const file = await service.files.create({
      requestBody: fileMetadata,
      media: media,
      fields: "id",
    });
    console.log("File Id:", file.data.id);
    return file.data.id;
  } catch (err) {
    // TODO(developer) - Handle error
    throw err;
  }

and the error log from the console:

chunk-OCKVXT67.js?v=fbea541c:2930 <Suspense> is an experimental feature and its API will likely change.
InspectorContainer.vue:35 messing up here?
chunk-APNZTO2G.js?v=fbea541c:16065 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'isTTY')
    at _Colours.isEnabled (chunk-APNZTO2G.js?v=fbea541c:16065:23)
    at _Colours.refresh (chunk-APNZTO2G.js?v=fbea541c:16068:37)
    at node_modules/google-logging-utils/build/src/colours.js (chunk-APNZTO2G.js?v=fbea541c:16109:13)
    at __require (chunk-4RDJM6KU.js?v=fbea541c:18:50)
    at node_modules/google-logging-utils/build/src/logging-utils.js (chunk-APNZTO2G.js?v=fbea541c:16157:21)
    at __require (chunk-4RDJM6KU.js?v=fbea541c:18:50)
    at node_modules/google-logging-utils/build/src/index.js (chunk-APNZTO2G.js?v=fbea541c:16405:18)
    at __require (chunk-4RDJM6KU.js?v=fbea541c:18:50)
    at node_modules/gcp-metadata/build/src/index.js (chunk-APNZTO2G.js?v=fbea541c:16446:18)
    at __require (chunk-4RDJM6KU.js?v=fbea541c:18:50)


(Note: I'm using node.js and vue).
Share Improve this question edited Mar 25 at 21:30 Moosterbox asked Mar 25 at 18:12 MoosterboxMoosterbox 11 bronze badge 1
  • It's unclear what is your case and what is the context for the snippet you posted. google-auth-library, etc are nodejs libraries, so is fs. The error suggests you're using it in client-side vue app, which you cannot do – Estus Flask Commented Mar 26 at 13:12
Add a comment  | 

1 Answer 1

Reset to default 0

It's not easy to guess what it could be with your small code snipped. Require is only needed if the used library export only in CommonJs (outdated) instead of ES6.

I was looking in the https://www.npmjs/package/google-auth-library docu it seems that many examples here are with require but one is with import

import { AwsClient, AwsSecurityCredentials, AwsSecurityCredentialsSupplier, ExternalAccountSupplierContext } from 'google-auth-library';

So the import shouldnt be a problem. Can you share more of your code ? Or even better a playground in stackblitz - sometimes it can even help to debug the issue yourself while creating a isolated stackblitz version.

本文标签: