Google-api-nodejs-client: Issue while using import { google } from 'googleapis' in Angular 6 Application

Created on 2 Aug 2018  路  15Comments  路  Source: googleapis/google-api-nodejs-client

Hi,

I have installed "npm install [email protected]" in my Angular 6 application to do service-to-service-authentication for Google Analytics API. I am following below article to achieve this.

https://libraries.io/npm/googleapis#service-to-service-authentication

However when I am including import { google } from 'googleapis'; or like const { OAuth2Client } = require('google-auth-library'); in .ts file in service class while ng build I am getting multiple errors

ERROR in ./node_modules/gtoken/node_modules/mime/index.js
ERROR in ./node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'child_process' in
ERROR in ./node_modules/google-auth-library/build/src/auth/googleauth.js

Thanks!

blocked question web

Most helpful comment

Greetings! Is it possible you're trying to use this npm module from the client? Are you bundling this via webpack or something? That's not currently supported, but is on it's way over in https://github.com/google/google-auth-library-nodejs/pull/371.

All 15 comments

Greetings! Is it possible you're trying to use this npm module from the client? Are you bundling this via webpack or something? That's not currently supported, but is on it's way over in https://github.com/google/google-auth-library-nodejs/pull/371.

Hi Justin, I am using npm module from Angular 6 application. I am using CLI to build the package not webpack.

@shiven80 the cli uses webpack but I do face the same problem when using it in a server application written in typescript.

Hi @JustinBeckwith, I'd like to bump this as I'm still getting import errors from compiling with tsc after installing via npm.

drew@local$ tsc
src/GoogleApiWrapper.ts:2:35 - error TS2307: Cannot find module 'googleapis'.

2 import { google, sheets_v4 } from 'googleapis';
                                    ~~~~~~~~~~~~

src/GoogleAuthWrapper.ts:9:24 - error TS2307: Cannot find module 'googleapis'.

9 import { google } from 'googleapis';
                         ~~~~~~~~~~~~

src/GoogleAuthWrapper.ts:11:30 - error TS2307: Cannot find module 'googleapis-common'.

11 import { OAuth2Client } from 'googleapis-common';
                                ~~~~~~~~~~~~~~~~~~~

Is there any ETA on this? I'd like to integrate this SDK in an Angular app

Nope, there is not sadly

@vmasek @shiven80 you might be able to create a working module using web-pack, which you could then import into angular. To do so you would use configuration something like this, in tsconfig.js:

module.exports = {
    resolve: {
        extensions: ['.js'],
        alias: {
            fs: path.resolve(__dirname, 'src/mock-fs.js'),
            child_process: path.resolve(__dirname, 'src/mock/mock-child-process.js')
        }
    }
};

where modck-fs.js, mock-child-process.js, etc., are stubbed out Node.js APIs:

module.exports = {
  readFileSync () {}
}

With a bit of work I think you could build a workaround using this approach; unfortunately there's no ETA right now on when we'll be able to add first-class browser support to all our libraries.

I've created a blog post on how to integrate this SDK with Angular, thank you for your help @bcoe

https://blog.angularindepth.com/google-apis-with-angular-214fadb8fbc5

@vmasek this is great :tada: I'm going to reference the heck out of this article.

@vmasek is your email on GitHub a good way to reach you with some feedback on your article?

yes

I have the same problem, only with Angular 8. I described my problem on stackoverflow:
https://stackoverflow.com/questions/59355718/how-to-use-google-sheets-api-in-angular-8

@vmasek thank you for the hack, but I hope the hack fix (or some other fix) will be part of the offical googleapis package.

Is there any plan to support googleapis with angular/webpack.

I've created a blog post on how to integrate this SDK with Angular, thank you for your help @bcoe

https://blog.angularindepth.com/google-apis-with-angular-214fadb8fbc5

I think your SSL certificate expired, I can't access your article.

I too have the same problem but I'm using React/Webpack. It seems to be webpack-able now (see https://github.com/googleapis/google-auth-library-nodejs/pull/371) but somehow I'm still getting a Can't resolve child_process in ... /google-auth-library/... though.

@gjj thanks for reaching out. It seems to be a problem with Angular in depth blog. Here is a link to medium
https://medium.com/angular-in-depth/google-apis-with-angular-214fadb8fbc5

Greetings, we're closing this due to inactivity. Please let us know if the issue needs to be reopened.

Was this page helpful?
0 / 5 - 0 ratings