简体   繁体   中英

Using a npm package inside a NodeJS module

I am trying to use googleapis npm package in my module but I am getting following error:

var gmail = google.gmail('v1');
                       ^

TypeError: google.gmail is not a function
    at Object.watchInbox (/home/ubuntu/workspace/functions/google_cloud.js:19:24)
    at /home/ubuntu/workspace/bootstrap.js:54:38
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:446:3)

Here is the module code:

(google_cloud.js)

const google = require('googleapis');

/**
 * Watch the Gmail Inbox
 *
 * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
 */
function watchInbox(auth) {
  ..
  var gmail = google.gmail('v1');
  gmail.users.watch(options, function (err, response) {
    ..
  }
  ..
}


module.exports = {
  watchInbox
}

Here is the parent file code:

(index.js)

var googleCloudModules = require("./functions/google_cloud.js");
..    
googleCloudModules.watchInbox(auth)
..

Any ideas on what I am doing wrong? Thanks in advance.

I had to use:

const {google} = require('googleapis'); 

This fixed it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM