Reputation: 63
I am pretty new to React and I followed Heroku's guide to use Create-react-app to create a new react app. I want to use Google APIs to authenticate users through Google's OAuth2 and get the refresh token, so I followed the guide on Google APIs Node Client to install the 'googleapis' package with npm install googleapis --save
but after I imported googleapis with import {google} from 'googleapis'
, I got the following error when I hit npm start
:
Uncaught TypeError: Cannot convert undefined or null to object
at Function.getPrototypeOf (<anonymous>)
at ./node_modules/google-p12-pem/node_modules/pify/index.js.module.exports (index.js:75)
at Object../node_modules/google-p12-pem/build/src/index.js (index.js:6)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/gtoken/build/src/index.js (index.js:50)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/google-auth-library/build/src/auth/jwtclient.js (jwtclient.js:63)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/google-auth-library/build/src/auth/googleauth.js (googleauth.js:60)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/google-auth-library/build/src/index.js (index.js:18)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/googleapis/build/src/lib/googleapis.js (googleapis.js:60)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/googleapis/build/src/index.js (index.js:18)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../src/App.js (App.css?9a66:26)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../src/index.js (index.css?f255:26)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object.0 (registerServiceWorker.js:117)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at ./node_modules/ansi-regex/index.js.module.exports (bootstrap 8c14a9046e11e185076c:724)
at bootstrap 8c14a9046e11e185076c:724
What did I miss that cause this error?
Upvotes: 6
Views: 3224
Reputation: 2268
The mistake here is rendering it on the client-side, not on the server-side. The googleapis library is not meant to run on the browser but in your node application server.
Here is the full answer (github)
Upvotes: 3
Reputation: 5669
You can use npm module 'gapi-client' instead of 'googleapis'.
Do an
npm install gapi-client
and in your code use like,
import gapi from 'gapi-client';
It works fine and you can use the same examples as mentioned in the google samples like https://developers.google.com/api-client-library/javascript/start/start-js
Upvotes: 6