TypeError: Cannot convert undefined or null to object
All I'm doing is: var {google} = require('googleapis') as the instructions by Google say to do.
I'm getting "Cannot convert undefined or null to object" error.
The error is happening around pify.
Tried to run the example in "Test googleapis in your browser" but it still fails.
Tried to manually add the required and included dependencies without success.
Running the latest node, npm, and googleapis release versions. What am I doing wrong here?
Tried both
import {google} from 'googleapis';
and
var {google} = require('googleapis')
Thanks!
I think the API has changed with the latest versions.
Can you try:
const {GoogleApis} = require('googleapis');
const google = new GoogleApis();
...
?
@alexander-fenster @JustinBeckwith Who should update all those obsolete documents?
Thanks jinwoo, but it's still erroring even with:
import {GoogleApis} from 'googleapis';`
Sorry I can't reproduce. And please scratch what I said above.
I can run this code from README without any problems.
const {google} = require('googleapis');
var urlshortener = google.urlshortener('v1');
Which version of googleapis are you using? And are you using any transpiler such as babel or TypeScript? Or pure JavaScript?
I don't think I am using anything special: Here is my package.json:
{
"name": "green-framingham",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"bootstrap": "^4.0.0",
"googleapis": "^26.0.1",
"json-server": "^0.12.1",
"moment": "^2.20.1",
"npm-run-all": "^4.1.2",
"prop-types": "^15.6.0",
"react": "^16.2.0",
"react-cookie": "^2.1.4",
"react-dom": "^16.2.0",
"react-fontawesome": "^1.6.1",
"react-google-maps": "^9.4.5",
"react-redux": "^5.0.7",
"react-router-dom": "^4.2.2",
"reactstrap": "^5.0.0-beta",
"recompose": "^0.26.0",
"redux": "^3.7.2",
"redux-immutable-state-invariant": "^2.1.0",
"redux-thunk": "^2.2.0",
"scriptjs": "^2.5.8",
"toastr": "^2.1.4",
"uuid": "^3.2.1"
},
"devDependencies": {
"react-scripts": "1.1.1"
},
"scripts": {
"start": "npm-run-all --parallel api go ",
"go": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"api": "json-server --watch tools/db.json --port 3006"
}
}
Apologies for the issues! I'm fairly certain we fixed the pify issue in 27, which we released yesterday. Could you upgrade to 27 and give that a shot?
What even is this? v27.0.0:
> let G = require('googleapis')
undefined
> G
{ GoogleApis: [Function: GoogleApis],
google:
GoogleApis {
_discovery: Discovery { transporter: DefaultTransporter {}, options: [Object] },
auth:
AuthPlus {
checkIsGCE: undefined,
jsonContent: null,
cachedCredential: null,
_cachedProjectId: null,
keyFilename: undefined,
scopes: undefined,
JWT: [Function: JWT],
Compute: [Function],
OAuth2: [Function] },
_options: {},
GeneratedAPIs: [Function: bound GeneratedAPIs] } }
Version 25 (I just picked an old one, doesn't matter) is correct:
> let G = require('googleapis')
undefined
> G
GoogleApis {
_options: {},
abusiveexperiencereport: [Function: bound abusiveexperiencereport],
acceleratedmobilepageurl: [Function: bound acceleratedmobilepageurl],
adexchangebuyer: [Function: bound adexchangebuyer],
adexchangebuyer2: [Function: bound adexchangebuyer2],
adexchangeseller: [Function: bound adexchangeseller],
adexperiencereport: [Function: bound adexperiencereport],
admin: [Function: bound admin],
adsense: [Function: bound adsense],
[lots of other stuff]
Same thing happened to a different package, same exact thing, I'll dig deeper, see if I can figure it out but I'm just an amateur. Thanks!
Seems like it broke when you added src/index.ts no idea why, I don't use any of that transpiling stuff. But yeah, what you're exporting there:
import {GoogleApis} from './lib/googleapis';
const google = new GoogleApis();
export {google, GoogleApis};
Matches what I get when I require the package. Was I supposed to change my code too? I don't see any warning anywhere.
@MySidesTheyAreGone v26 was a semver major change and you are supposed to change the code. Please see the release note: https://github.com/google/google-api-nodejs-client/releases/tag/v26.0.0
I think we can close this issue now. Closing.
OK thanks
@jinwoo, @MySidesTheyAreGone, @JustinBeckwith
You are right, the issue is with the pify library, not with you. I am still getting the same issue in version 27
Pify: TypeError when passing undefined or null #58
I still don't know how to fix this from my end and am waiting for them to release a fixed version. Since it is a part of your build you might want to make sure it works or replace the dependency.
Thanks,
@weisssean I don't think anyone but you can reproduce your issue with version 27. Just to make sure, are you running this on the server side or on the browser side? I don't think this library runs on the browser.
Ahh, I am running it on the browser side in React. I will try to implement it in my server.js instead
Here's someone else running into the same issue: error-using-create-react-app-with-googleapis
Works fine, I was rendering on the wrong side of life :)
@weisssean, Is it possible to share solution. As i am trying same, how to run server within react app. Is it creating a server on other port etc. If you can share the solution that will be helpful.
If I remember correctly all it was was that I tried to import and use this on the client instead of the server. What you want to do is create an express.js server and routes for your app and implement it for those routes. There are many tutorials on how to create server side for your application, it's pretty easy (the basics at least). Good luck.