You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TypeScript typings cause very high memory usage in tsc #2187
When importing googleapis within a TypeScript project, tsc takes up a large amount of memory (500 MB+). This appears to be the case because when googleapis is imported, it imports the type definition files for every single API that Google has, even if only a single API (e.g. drive_v3) is required.
Environment details
OS: Ubuntu 20.04 LTS
Node.js version: 10.20.1 (LTS)
npm version: N/A (We don't use npm instally, but we use yarn v1.22.4)
If splitting the module up to individual packages is going to be a massive undertaking, I think it would be also helpful if there will be an officially supported way of including individual modules, like the workaround above. I'm concerned that for example, AuthPlus would be an internal implementation detail.
Requiring an additional 500 MB of memory per build job using tsc just to use one of Google APIs is quite a huge ask. (The alternative is to use the REST APIs directly, but we lose type checking)
Yeah, there are a lot of tradeoffs here to be sure. We used to dynamically discover available APIs in the module at runtime, but this badly broke things like webpack and rollup. We can't really reduce the raw volume of TypeScript types on a per-API level, because that gets us in trouble with correctness.
The good news here is that we're actually pretty close to having split modules (20% left is 80% of the work), and it's possible you can take advantage of what we have so far. You can cd into any API dir, and run npm pack to get a single standalone module with just that API. For example:
cd src/apis/drive
npm install
npm pack
That actually should give you a self contained module that only contains what you need. I know this ain't ideal, but it does provide a way to get the isolated module.
I had trouble packing the relevant packages.
Eventually I mapped googleapis package types to an empty types file and the memory issue was resolved.
Of course, no types support this way.
@rommguy are you able to import the APIs that you're using individually, as @yjwong suggests. I believe this is partially a documentation problem, as we should be documenting this as the recommended approach.
Getting an exception: TypeError: google.iam is not a constructor
I'm guessing I imported the wrong property or from the wrong path, but I have no idea what should be the correct way.
@bcoe Thank you, I really appreciate your help!
I'm using several of the apis, and I think it would be best if there was some documentation of how to import each service separately.
I'll try what you suggested about the iam API.
This OOM problem is a serious issue for adoption of this library! I wasn't able to tsc a small project (using 512mb memory, which should be enough) that uses 1 api (sheets) due to this.
Sheet-specific workaround for others that view this:
I'm was shocked to find this issue in my app after a ton of digging. I feel like there should be a major callout to use the individual modules since it's written in typescript.
If you need to reduce startup times
doesn't describe why to use the smaller libraries. It caused my app to crash due to memory no matter what i tried if i imported anything.
Reacted by Diana Ionita, Daniel Burgess, Leonard Q. Marcq and Patrick Hübl-Neschkudla
This is still a huge issue. It's really hard to pin down since tools like PNPM and Turborepo do not tell you that the process quit because of a memory surge. Most NodeJS devs do not know that 512m is the memory limit for Node processes.
Can you guys please add some exports to your package.json? Things like googleapis/youtube and googleapis/oauth? Having devs find the build artifacts in the path is a problem since the path values could change at any time.
For now you can use NODE_OPTIONS to force a higher limit. I know my project goes close to 2GB.
Just bumping this, similar to the point that @matsko brought up, it was really difficult to pin down that this was the issue that was causing it. Exports to core aspects such as oauth would be super helpful. Many thanks!
I ran into this problem when using Jest: importing the big library causes Jest to fill up the entire system memory (32GB in my case) within seconds because it starts 16 workers (one per CPU) that all compile in parallel. So these typings froze my entire computer everytime I ran Jest. What a nightmare to debug!
You should really document the individual API libraries as the recommended way to consume these APIs. And deprecate/remove the big library.
Activity
JustinBeckwith commentedon May 27, 2020
Short of splitting the module up into their own sub-modules, I doubt there's anything that we could really do about this :/
yjwong commentedon May 27, 2020
@JustinBeckwith I'm guessing that would be related to this issue? #806
If splitting the module up to individual packages is going to be a massive undertaking, I think it would be also helpful if there will be an officially supported way of including individual modules, like the workaround above. I'm concerned that for example,
AuthPluswould be an internal implementation detail.Requiring an additional 500 MB of memory per build job using
tscjust to use one of Google APIs is quite a huge ask. (The alternative is to use the REST APIs directly, but we lose type checking)JustinBeckwith commentedon May 27, 2020
Yeah, there are a lot of tradeoffs here to be sure. We used to dynamically discover available APIs in the module at runtime, but this badly broke things like webpack and rollup. We can't really reduce the raw volume of TypeScript types on a per-API level, because that gets us in trouble with correctness.
The good news here is that we're actually pretty close to having split modules (20% left is 80% of the work), and it's possible you can take advantage of what we have so far. You can cd into any API dir, and run
npm packto get a single standalone module with just that API. For example:That actually should give you a self contained module that only contains what you need. I know this ain't ideal, but it does provide a way to get the isolated module.
rommguy commentedon Sep 28, 2020
I had trouble packing the relevant packages.
Eventually I mapped googleapis package types to an empty types file and the memory issue was resolved.
Of course, no types support this way.
bcoe commentedon Sep 30, 2020
@rommguy are you able to import the APIs that you're using individually, as @yjwong suggests. I believe this is partially a documentation problem, as we should be documenting this as the recommended approach.
rommguy commentedon Oct 1, 2020
@bcoe I wasn't able to do as @yjwong recommended.
I'm using version 59.0.0
Example -
Now I tried importing like so -
Getting an exception: TypeError: google.iam is not a constructor
I'm guessing I imported the wrong property or from the wrong path, but I have no idea what should be the correct way.
bcoe commentedon Oct 1, 2020
@rommguy I believe you can use an API directly like this:
Let me know if this works for you, and if it's an approach that's viable, we can work on making this easier to do.
rommguy commentedon Oct 1, 2020
@bcoe Thank you, I really appreciate your help!
I'm using several of the apis, and I think it would be best if there was some documentation of how to import each service separately.
I'll try what you suggested about the iam API.
sgb-io commentedon Mar 22, 2021
Thanks @bcoe for the workaround.
This OOM problem is a serious issue for adoption of this library! I wasn't able to
tsca small project (using 512mb memory, which should be enough) that uses 1 api (sheets) due to this.Sheet-specific workaround for others that view this:
bcoe commentedon Mar 30, 2021
@sgb-io @rommguy we have now also started publishing individual modules for any libraries in googleapis that don't have an alternative client:
Give these a shot, rather than the hack I proposed?
geoguide commentedon Feb 6, 2023
I'm was shocked to find this issue in my app after a ton of digging. I feel like there should be a major callout to use the individual modules since it's written in typescript.
doesn't describe why to use the smaller libraries. It caused my app to crash due to memory no matter what i tried if i imported anything.
matsko commentedon Jan 8, 2024
This is still a huge issue. It's really hard to pin down since tools like PNPM and Turborepo do not tell you that the process quit because of a memory surge. Most NodeJS devs do not know that 512m is the memory limit for Node processes.
Can you guys please add some
exportsto yourpackage.json? Things likegoogleapis/youtubeandgoogleapis/oauth? Having devs find the build artifacts in the path is a problem since the path values could change at any time.For now you can use
NODE_OPTIONSto force a higher limit. I know my project goes close to 2GB.NODE_OPTIONS="--max-old-space-size=4096" pnpm tsc --project ./tsconfig.jsonandrewliu-rr commentedon Jun 24, 2025
Just bumping this, similar to the point that @matsko brought up, it was really difficult to pin down that this was the issue that was causing it. Exports to core aspects such as oauth would be super helpful. Many thanks!
noahnu commentedon Jun 25, 2025
I've created a dedicated feature request for the googleapis/oauth request here: #3695
karottenreibe commentedon Feb 27, 2026
I ran into this problem when using Jest: importing the big library causes Jest to fill up the entire system memory (32GB in my case) within seconds because it starts 16 workers (one per CPU) that all compile in parallel. So these typings froze my entire computer everytime I ran Jest. What a nightmare to debug!
You should really document the individual API libraries as the recommended way to consume these APIs. And deprecate/remove the big library.