NodeJS - Google API Module - [TypeError: Cannot re

2019-08-12 03:26发布

I installed the nodejs module "googleapis" (npm install googleapis) to an Azure Mobile Services instance and I wanted to do a quick test with some sample code I found here https://www.npmjs.org/package/googleapis. After trying the code sample which creates a text file in google drive I get the following error message in the console:

[TypeError: Cannot read property 'prototype' of undefined]

The error is occurring at line 40 of the current version of google-api-nodejs-client / apis / index.js (https://github.com/google/google-api-nodejs-client/blob/master/apis/index.js):

var Endpoint = require(__dirname + '/' + filename + '/' + path.basename(version));

When debugging I can see the path used in the above method as the following:

D:\home\site\wwwroot\App_Data\config\scripts\node_modules\googleapis\apis\drive\v2

I have verified that the path is correct.

Here is the code I tried to run:

var google = require('googleapis');

var OAuth2Client = google.auth.OAuth2;
var CLIENT_ID = 'xxxx';
var CLIENT_SECRET = 'xxxxxx';
var REDIRECT_URL = 'xxxxx';
var oauth2Client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
oauth2Client.setCredentials({
access_token: identities.google.accessToken
});

var drive = google.drive({ version: 'v2', auth: oauth2Client });

drive.files.insert({
  resource: {
    title: 'Test',
    mimeType: 'text/plain'
  },
  media: {
    mimeType: 'text/plain',
    body: 'Hello World'
  }
}, function(err, response) {
console.log('error:', err, 'inserted:', response.id);
});

What is the source of the issue?

Here is the error stack trace:

TypeError: Cannot read property 'prototype' of undefined at Object.<anonymous> 
(D:\home\site\wwwroot\App_Data\config\scripts\node_modules\googleapis\node_modules\multipart-stream\node_modules\sandwich-stream\lib\sandwich-stream.js:24:50) 
    at Module._compile (module.js:449:26) 
    at Object.Module._extensions..js (module.js:467:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Module.require (module.js:362:17) 
    at require (module.js:378:17) 
    at Object.<anonymous> (D:\home\site\wwwroot\App_Data\config\scripts\node_modules\googleapis\node_modules\multipart-stream\index.js:1:78) 
    at Module._compile (module.js:449:26) 
    at Object.Module._extensions..js (module.js:467:10)

Here is line 24 of sandwich-stream.js:

SandwichStream.prototype = Object.create(Readable.prototype, {
  constructor: SandwichStream
});

The nodejs version is:

Node version: v0.8.28

1条回答
甜甜的少女心
2楼-- · 2019-08-12 03:48

My guess is that Azure Mobile Services is doing something funky with __dirname that is making the file path not resolve correctly. Can you check the logs where you are running this? The actual error should be console.log'd from this line: https://github.com/google/google-api-nodejs-client/blob/master/apis/index.js#L45

查看更多
登录 后发表回答